初始代码:
async def myRequest(data):
url = data['url']
header = data['header']
country = data['country']
category = data['category']
async with aiohttp.ClientSession(headers = header) as session:
async with await session.get(url = url) as resp:
html = await resp.read()
today = str(datetime.date.today())
filePath = '%s.xlsx'

文章讨论了在使用Python的aiohttp库进行异步HTTP请求时遇到的网络错误,如ClientOSError和ServerDisconnectedError。作者分析这些错误可能由于每个任务创建独立的session导致,并提出了优化方案——在主函数中创建单个ClientSession实例,复用session以减少资源消耗。这种方法提高了代码效率并减少了错误发生。
最低0.47元/天 解锁文章
909

被折叠的 条评论
为什么被折叠?



