美文网首页
并发异常,ValueError: too many file d

并发异常,ValueError: too many file d

作者: 旅行小张 | 来源:发表于2020-06-22 09:22 被阅读0次

分析:

报错的原因字面上看是 Python 调取的 select 对打开的文件有最大数量的限制,这个其实是操作系统的限制,linux打开文件的最大数默认是1024,windows默认是509,超过了这个值,程序就开始报错

解决办法:

限制并发量,此处示例将并发量设置为500

#coding:utf-8
import time,asyncio,aiohttp
 
 
url = 'https://www.baidu.com/'
async def hello(url,semaphore):
    async with semaphore:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                return await response.read()
 
 
async def run():
    semaphore = asyncio.Semaphore(500) # 限制并发量为500
    to_get = [hello(url.format(),semaphore) for _ in range(1000)] #总共1000任务
    await asyncio.wait(to_get)
 
 
if __name__ == '__main__':
#    now=lambda :time.time()
    loop = asyncio.get_event_loop()
    loop.run_un
til_complete(run())
    loop.close()

完成!

来源:https://mohen.blog.csdn.net/article/details/106592044

相关文章

网友评论

      本文标题:并发异常,ValueError: too many file d

      本文链接:https://www.haomeiwen.com/subject/seotfktx.html