我用asyncio
又漂亮aiohttp
。主要思想是我向服务器发出请求(它返回链接),然后我想从所有链接并行下载文件(类似于示例)。
代码:
import aiohttp
import asyncio
@asyncio.coroutine
def downloader(file):
print('Download', file['title'])
yield from asyncio.sleep(1.0) # some actions to download
print('OK', file['title'])
def run():
r = yield from aiohttp.request('get', 'my_url.com', True))
raw = yield from r.json()
tasks = []
for file in raw['files']:
tasks.append(asyncio.async(downloader(file)))
asyncio.wait(tasks)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
但是,当我尝试运行它时,我有很多“下载...”输出和
Task was destroyed but it is pending!
和'OK +文件名'无关。
我该如何解决?