【发布时间】:2022-01-07 00:06:49
【问题描述】:
For every 10 minutes, do the following tasks.
- generate list of image urls to download
- (if previous download is not finished, we have to cancel the download)
- download images concurrently
我对协程比较陌生.. 我可以用协程构建上述内容吗?
我认为协程本质上是顺序流.. 所以想起来有问题..
其实我自己想想,跟着就行了?
async def generate_urls():
await sleep(10)
result = _generate_urls()
return result
async def download_image(url):
# download images
image = await _download_image()
return image
async def main():
while True:
urls = await generate_urls()
for url in urls:
download_task = asyncio.create_task(download_image(url))
await download_task
asyncio.run(main())
【问题讨论】:
标签: python async-await coroutine