Windows下asyncio+aiohttp运行报select属性错误及并发限制问题
Windows环境下aiohttp异步爬取触发
AttributeError: module 'select' has no attribute 'select' 问题说明 运行场景与已知信息
- 运行载体:Windows 个人本地电脑
- 触发报错:asyncio 流程运行中抛出
AttributeError: module 'select' has no attribute 'select' - 已知环境特性:Windows 系统下asyncio默认事件循环存在64个套接字的使用上限,无法确定该限制是否为本次报错的诱因
- 待运行的原始代码:
import aiohttp import asyncio import time async def download_file(url): print(f'started downloading{url}') connector = aiohttp.TCPConnector(limit=60) async with aiohttp.clientSession(connector) as session: async with session.get(url) as resp: content = await resp.read() print (f'Finished download{url}') return content async def write_file(n, content): filename = f'async_{n}.html' with open(filename,'wb') as f: print(f'started writing{filename}') f.write(content) print(f'Finished writing{filename}') async def scrape_task(n,url): content = await download_file(url) await write_file(n,content) async def main(): tasks = [] for n,url in enumerate(open('urls.txt').readlines()): tasks.append((scrape_task(n, url))) await asyncio.wait(tasks) if __name__ == '__main__': t=time.perf_counter() loop = asyncio.get_event_loop() loop.run_until_complete(main()) t2 = time.perf_counter() - t print(f'Total time taken: {t2:0.2f} seconds') - 已做的代码修改尝试:在下载函数内部添加TCPConnector实例设置,尝试将全局并发连接数限制为60:
connector = aiohttp.TCPConnector(limit=60) async with aiohttp.clientSession(connector) as session: - 当前状态:始终无法定位代码异常根因
核心异常根因
- 事件循环适配不兼容
Windows 系统下 Python 3.8+ 默认采用ProactorEventLoop作为asyncio的底层事件循环,部分版本的aiohttp在适配该循环时会错误调用类Unix系统专属的select.select接口,最终抛出模块无对应属性的报错。
此前认知里的64套接字限制,是旧版Windows下SelectorEventLoop的轮询上限,和当前select属性报错没有直接因果关系。 - 代码逻辑本身存在多处缺陷
- 连接池作用域错误:将
TCPConnector实例定义在download_file函数内部,每一个下载任务都会单独创建独立的连接池,完全无法实现全局60并发的限制,反而会重复创建大量连接实例,拉高系统套接字占用 - 类名拼写错误:代码中使用的
aiohttp.clientSession首字母未大写,正确类名为aiohttp.ClientSession,拼写错误会直接触发后续的类属性查找失败 - 输入未做清洗:读取urls.txt时直接使用文件行内容,未去除每行自带的换行符,可能间接引发请求连接异常
- 连接池作用域错误:将
修复方案
- 在程序主入口前显式设置适配aiohttp的Windows事件循环策略,从根源避免select属性报错:
if __name__ == '__main__': # Windows下切换为SelectorEventLoop,适配aiohttp运行 asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) t=time.perf_counter() loop = asyncio.get_event_loop() loop.run_until_complete(main()) t2 = time.perf_counter() - t print(f'Total time taken: {t2:0.2f} seconds') - 调整连接池与会话的作用域,将
TCPConnector和ClientSession实例移至主任务层级统一创建,真正实现全局60并发的连接限制:async def main(): # 统一创建全局连接池与会话 connector = aiohttp.TCPConnector(limit=60) async with aiohttp.ClientSession(connector=connector) as session: tasks = [] for n, url in enumerate(open('urls.txt').readlines()): # 传入清洗后的url和全局会话给爬取任务 tasks.append(scrape_task(n, url.strip(), session)) await asyncio.wait(tasks) async def scrape_task(n, url, session): content = await download_file(url, session) await write_file(n, content) async def download_file(url, session): print(f'started downloading {url}') async with session.get(url) as resp: content = await resp.read() print(f'Finished download {url}') return content
内容的提问来源于stack exchange,提问作者nikhil davis
相关产品推荐
相关产品推荐

