【问题标题】:Why i need cancel tasks in this queue example?为什么我需要在此队列示例中取消任务?
【发布时间】:2019-06-12 15:50:29
【问题描述】:

嗯,我正在研究 python 文档来学习我的工作。我是 python 和编程新手,我也不太了解异步操作等编程概念。

我使用 Fedora 29 和 Python 3.7.3 来尝试队列和 lib asyncio 的示例。

按照下面的队列和异步操作示例:

import asyncio
import random
import time


async def worker(name, queue):
    while True:
        # Get a "work item" out of the queue.
        sleep_for = await queue.get()

        # Sleep for the "sleep_for" seconds.
        await asyncio.sleep(sleep_for)

        # Notify the queue that the "work item" has been processed.
        queue.task_done()

        print(f'{name} has slept for {sleep_for:.2f} seconds')


async def main():
    # Create a queue that we will use to store our "workload".
    queue = asyncio.Queue()

    # Generate random timings and put them into the queue.
    total_sleep_time = 0
    for _ in range(20):
        sleep_for = random.uniform(0.05, 1.0)
        total_sleep_time += sleep_for
        queue.put_nowait(sleep_for)

    # Create three worker tasks to process the queue concurrently.
    tasks = []
    for i in range(3):
        task = asyncio.create_task(worker(f'worker-{i}', queue))
        tasks.append(task)

    # Wait until the queue is fully processed.
    started_at = time.monotonic()
    await queue.join()
    total_slept_for = time.monotonic() - started_at

    # Cancel our worker tasks.
    for task in tasks:
        task.cancel()
    # Wait until all worker tasks are cancelled.
    await asyncio.gather(*tasks, return_exceptions=True)

    print('====')
    print(f'3 workers slept in parallel for {total_slept_for:.2f} seconds')
    print(f'total expected sleep time: {total_sleep_time:.2f} seconds')

asyncio.run(main())

为什么在这个例子中我需要取消任务?为什么我可以排除这部分代码

# Cancel our worker tasks.
    for task in tasks:
        task.cancel()
    # Wait until all worker tasks are cancelled.
    await asyncio.gather(*tasks, return_exceptions=True)

示例工作正常吗?

【问题讨论】:

    标签: python-3.x queue python-asyncio


    【解决方案1】:

    为什么在这个例子中我需要取消任务?

    因为否则它们将无限期地挂起,等待队列中永远不会到达的新项目。在那个特定的示例中,无论如何您都将退出事件循环,因此它们“挂起”并没有什么害处,但是如果您将其作为实用函数的一部分进行,则会造成协程泄漏。

    换句话说,取消工作人员会告诉他们退出,因为不再需要他们的服务,并且需要确保与他们相关的资源被释放。

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 2017-06-19
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 2014-01-31
      • 1970-01-01
      相关资源
      最近更新 更多