Maison  >  Article  >  développement back-end  >  Comment obtenir les tâches en cours et en cours d'exécution en Python de manière asynchrone

Comment obtenir les tâches en cours et en cours d'exécution en Python de manière asynchrone

PHPz
PHPzavant
2023-05-12 14:43:171415parcourir

正文

我们可以反省在 asyncio 事件循环中运行的任务。这可以通过为当前运行的任务和所有正在运行的任务获取一个 asyncio.Task 对象来实现。

1. 如何获取当前任务

我们可以通过 asyncio.current_task() 函数获取当前任务。此函数将为当前正在运行的任务返回一个任务对象。

...
# get the current task
task = asyncio.current_task()
  • 传递给 asyncio.run() 的主协程。

  • 通过 asyncio.create_task() 在 asyncio 程序中创建和调度的任务。

一个任务可以创建并运行另一个协程(例如,不包含在任务中)。从协程中获取当前任务将为正在运行的任务返回一个 Task 对象,但不会返回当前正在运行的协程。

如果协程或任务需要有关自身的详细信息,例如用于日志记录的任务名称,则获取当前任务会很有帮助。

我们可以探索如何为用于启动 asyncio 程序的主协程获取 Task 实例。下面的示例定义了一个用作程序入口点的协程。它报告一条消息,然后获取当前任务并报告其详细信息。

这是第一个重要的示例,因为它强调所有协程都可以作为异步事件循环中的任务进行访问。

下面列出了完整的示例。

# SuperFastPython.com
# example of getting the current task from the main coroutine
import asyncio
# define a main coroutine
async def main():
    # report a message
    print('main coroutine started')
    # get the current task
    task = asyncio.current_task()
    # report its details
    print(task)
# start the asyncio program
asyncio.run(main())

运行该示例首先创建主协程并使用它来启动 asyncio 程序。main() 协程运行并首先报告一条消息。

然后它检索当前任务,这是一个代表自身的任务对象,即当前正在运行的协程。然后它会报告当前正在运行的任务的详细信息。

我们可以看到该任务具有第一个任务的默认名称“Task-1”,并且正在执行 main() 协程,即当前正在运行的协程。

这突出表明我们可以使用 asyncio.current_task() 函数来访问当前正在运行的协程的任务对象,该对象自动包装在任务对象中。

main coroutine started
<Task pending name=&#39;Task-1&#39; coro=<main() running at ...> cb=[_run_until_complete_cb() at ...]>

2. 如何获取所有任务

我们可能需要访问异步程序中的所有任务。这可能有很多原因,例如:

  • 反省程序的当前状态或复杂性。

  • 记录所有正在运行的任务的详细信息。

  • 查找可以查询或取消的任务。

我们可以通过 asyncio.all_tasks() 函数在 asyncio 程序中获取一组所有已计划和正在运行(尚未完成)的任务。

...
# get all tasks
tasks = asyncio.all_tasks()

这将返回 asyncio 程序中所有任务的集合。它是一个集合,因此每个任务只代表一次。

如果出现以下情况,将包括一项任务:

  • 任务已安排但尚未运行。

  • 该任务当前正在运行(例如,但当前已暂停)

该集合还将包括当前正在运行的任务的任务,例如正在执行调用 asyncio.all_tasks() 函数的协程的任务。

另外,回想一下用于启动 asyncio 程序的 asyncio.run() 方法会将提供的协程包装在任务中。这意味着所有任务的集合将包括程序入口点的任务。

我们可以探索在一个 asyncio 程序中有很多任务的情况,然后得到一组所有任务。

在此示例中,我们首先创建 10 个任务,每个任务包装并运行相同的协程。主协程然后获取程序中计划或运行的所有任务的集合并报告它们的详细信息。

下面列出了完整的示例。

# SuperFastPython.com
# example of starting many tasks and getting access to all tasks
import asyncio
# coroutine for a task
async def task_coroutine(value):
    # report a message
    print(f&#39;task {value} is running&#39;)
    # block for a moment
    await asyncio.sleep(1)
# define a main coroutine
async def main():
    # report a message
    print(&#39;main coroutine started&#39;)
    # start many tasks
    started_tasks = [asyncio.create_task(task_coroutine(i)) for i in range(10)]
    # allow some of the tasks time to start
    await asyncio.sleep(0.1)
    # get all tasks
    tasks = asyncio.all_tasks()
    # report all tasks
    for task in tasks:
        print(f&#39;&gt; {task.get_name()}, {task.get_coro()}&#39;)
    # wait for all tasks to complete
    for task in started_tasks:
        await task
# start the asyncio program
asyncio.run(main())

运行该示例首先创建主协程并使用它来启动 asyncio 程序。main() 协程运行并首先报告一条消息。然后它创建并安排 10 个包装自定义协程的任务。然后 main() 协程会阻塞片刻以允许任务开始运行。任务开始运行,每个任务报告一条消息,然后休眠。

main() 协程恢复并获取程序中所有任务的列表。然后它报告每个的名称和协程。最后,它枚举已创建的任务列表并等待每个任务完成。

这突出表明我们可以获得 asyncio 程序中所有任务的集合,其中包括创建的任务以及代表程序入口点的任务。

coroutine principale démarrée
la tâche 0 est en cours d'exécution
la tâche 1 est en cours d'exécution
la tâche 2 est en cours d'exécution
la tâche 3 est en cours d'exécution
la tâche 4 est en cours d'exécution
la tâche 5 est en cours d'exécution
la tâche 6 est en cours d'exécution
la tâche 7 est en cours d'exécution
la tâche 8 est en cours d'exécution
la tâche 9 est en cours d'exécution
> Tâche-9, 8d25fd23bb9028bc7ea702339fdb3b4d
> Tâche-2, 5bda2b9bed94dbdb59d779a876697d37
> Tâche-11, 3049e3963b0d8418490aecf7d8ab84e3
> Tâche-7, 29700f859b4ebb5e7249a9dfd9316810
> Tâche-4, cc854a9ad22cf498d2d9bb6f428915ef
> Tâche-10, 74ac8bc6e005ab06b0b95c48ca7b8b20
> Tâche-8, 13cb77d73a3423baf6a83a1ff7c1a5ca
> Tâche-5, cbbd79d9d5807ede56eb1dcf9303b8e8
> Tâche-1, 7f6dced395f8c9527c989a311477a18f
> Tâche-3, 9088cb68515659a9bbfc62e2edbd7997
> Tâche-6, 9ec0339b1c2048ba2af293b0f97cec4b

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer