【问题标题】:python coroutine, perform tasks periodically and cancelpython协程,定期执行任务并取消
【发布时间】: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


    【解决方案1】:

    您当前的代码非常接近。以下是一些修改,以使其更符合您的原始规范:

    import asyncio
    def generate_urls():
      return _generate_urls() #no need to sleep in the URL generation function
    
    async def download_image(url):
      image = await _download_image()
      return image
    
    async def main():
       tasks = []
       while True:
          tasks.extend(t:=[asyncio.create_task(download_image(url)) for url in generate_urls()])
          await asyncio.gather(*t) #run downloads concurrently
          await asyncio.sleep(10) #sleep after creating tasks
          for i in d: #after 10 seconds, check if any of the downloads are still running
            if not i.done():
                i.cancel() #cancel if task is not complete
    

    【讨论】:

    • 有没有办法将消息发送到task.. 之一。有时我想将参数传递给任务,而不取消它。我可能会说,在我取消你之前,我会让你(任务)知道你还有多少分钟。当我告诉你剩下的时间时,请告诉我你是否认为你能按时完成。
    • @eugene 为此,您必须知道每个任务的下载进度。无论如何,这里实际上不需要某种形式的消息传递:您可以用一个类对象包装每个任务,该类对象可以控制对donecancel 的检查,还可以存储每个分配的任务还剩下多少秒/分钟.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多