我想创建一个从网站异步下载的功能。我需要将下载结果与输入参数结合起来,以便可以在下载后同时使用结果和参数。
我目前有以下内容:
async def download(session, url, var1, var2):
with async_timeout.timeout(10):
async with session.get(url) as response:
return await (response.read(), url, var1, var2)
async def loop_download(loop, urls, var1s, var2s):
async with aiohttp.ClientSession(loop=loop) as session:
tasks = [download(session, url, var1, var2) for url, var1, var2 in zip(urls, var1s, var2s)]
results = await asyncio.gather(*tasks)
return results
loop = asyncio.get_event_loop()
results = loop.run_until_complete(loop_download(loop, urls, var1s, var2s))
这将返回错误:
TypeError: object tuple can't be used in 'await' expression
如何将一些输入数据(例如url)加入结果,以便可以将其用于进一步分析?
答案 0 :(得分:2)
通过以下方式解决:
return (await response.read(), url, x, y)