Python?異步之如何獲取當前和正在運行任務詳解
正文
我們可以反省在 asyncio 事件循環(huán)中運行的任務。這可以通過為當前運行的任務和所有正在運行的任務獲取一個 asyncio.Task 對象來實現(xiàn)。
1. 如何獲取當前任務
我們可以通過 asyncio.current_task() 函數(shù)獲取當前任務。此函數(shù)將為當前正在運行的任務返回一個任務對象。
... # get the current task task = asyncio.current_task()
- 傳遞給 asyncio.run() 的主協(xié)程。
- 通過 asyncio.create_task() 在 asyncio 程序中創(chuàng)建和調(diào)度的任務。
一個任務可以創(chuàng)建并運行另一個協(xié)程(例如,不包含在任務中)。從協(xié)程中獲取當前任務將為正在運行的任務返回一個 Task 對象,但不會返回當前正在運行的協(xié)程。
如果協(xié)程或任務需要有關自身的詳細信息,例如用于日志記錄的任務名稱,則獲取當前任務會很有幫助。
我們可以探索如何為用于啟動 asyncio 程序的主協(xié)程獲取 Task 實例。下面的示例定義了一個用作程序入口點的協(xié)程。它報告一條消息,然后獲取當前任務并報告其詳細信息。
這是第一個重要的示例,因為它強調(diào)所有協(xié)程都可以作為異步事件循環(huán)中的任務進行訪問。
下面列出了完整的示例。
# 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())
運行該示例首先創(chuàng)建主協(xié)程并使用它來啟動 asyncio 程序。main() 協(xié)程運行并首先報告一條消息。
然后它檢索當前任務,這是一個代表自身的任務對象,即當前正在運行的協(xié)程。然后它會報告當前正在運行的任務的詳細信息。
我們可以看到該任務具有第一個任務的默認名稱“Task-1”,并且正在執(zhí)行 main() 協(xié)程,即當前正在運行的協(xié)程。
這突出表明我們可以使用 asyncio.current_task() 函數(shù)來訪問當前正在運行的協(xié)程的任務對象,該對象自動包裝在任務對象中。
main coroutine started <Task pending name='Task-1' coro=<main() running at ...> cb=[_run_until_complete_cb() at ...]>
2. 如何獲取所有任務
我們可能需要訪問異步程序中的所有任務。這可能有很多原因,例如:
- 反省程序的當前狀態(tài)或復雜性。
- 記錄所有正在運行的任務的詳細信息。
- 查找可以查詢或取消的任務。
我們可以通過 asyncio.all_tasks() 函數(shù)在 asyncio 程序中獲取一組所有已計劃和正在運行(尚未完成)的任務。
... # get all tasks tasks = asyncio.all_tasks()
這將返回 asyncio 程序中所有任務的集合。它是一個集合,因此每個任務只代表一次。
如果出現(xiàn)以下情況,將包括一項任務:
- 任務已安排但尚未運行。
- 該任務當前正在運行(例如,但當前已暫停)
該集合還將包括當前正在運行的任務的任務,例如正在執(zhí)行調(diào)用 asyncio.all_tasks() 函數(shù)的協(xié)程的任務。
另外,回想一下用于啟動 asyncio 程序的 asyncio.run() 方法會將提供的協(xié)程包裝在任務中。這意味著所有任務的集合將包括程序入口點的任務。
我們可以探索在一個 asyncio 程序中有很多任務的情況,然后得到一組所有任務。
在此示例中,我們首先創(chuàng)建 10 個任務,每個任務包裝并運行相同的協(xié)程。主協(xié)程然后獲取程序中計劃或運行的所有任務的集合并報告它們的詳細信息。
下面列出了完整的示例。
# 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'task {value} is running')
# block for a moment
await asyncio.sleep(1)
# define a main coroutine
async def main():
# report a message
print('main coroutine started')
# 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'> {task.get_name()}, {task.get_coro()}')
# wait for all tasks to complete
for task in started_tasks:
await task
# start the asyncio program
asyncio.run(main())
運行該示例首先創(chuàng)建主協(xié)程并使用它來啟動 asyncio 程序。main() 協(xié)程運行并首先報告一條消息。然后它創(chuàng)建并安排 10 個包裝自定義協(xié)程的任務。然后 main() 協(xié)程會阻塞片刻以允許任務開始運行。任務開始運行,每個任務報告一條消息,然后休眠。
main() 協(xié)程恢復并獲取程序中所有任務的列表。然后它報告每個的名稱和協(xié)程。最后,它枚舉已創(chuàng)建的任務列表并等待每個任務完成。
這突出表明我們可以獲得 asyncio 程序中所有任務的集合,其中包括創(chuàng)建的任務以及代表程序入口點的任務。
main coroutine started
task 0 is running
task 1 is running
task 2 is running
task 3 is running
task 4 is running
task 5 is running
task 6 is running
task 7 is running
task 8 is running
task 9 is running
> Task-9, <coroutine object task_coroutine at 0x10e186e30>
> Task-2, <coroutine object task_coroutine at 0x10e184e40>
> Task-11, <coroutine object task_coroutine at 0x10e186f10>
> Task-7, <coroutine object task_coroutine at 0x10e186d50>
> Task-4, <coroutine object task_coroutine at 0x10e185700>
> Task-10, <coroutine object task_coroutine at 0x10e186ea0>
> Task-8, <coroutine object task_coroutine at 0x10e186dc0>
> Task-5, <coroutine object task_coroutine at 0x10e186ab0>
> Task-1, <coroutine object main at 0x10e1847b0>
> Task-3, <coroutine object task_coroutine at 0x10e184f90>
> Task-6, <coroutine object task_coroutine at 0x10e186ce0>
接下來,我們將探討如何同時運行多個協(xié)程。
以上就是Python 異步之如何獲取當前和正在運行任務詳解的詳細內(nèi)容,更多關于Python 異步獲取當前運行任務的資料請關注腳本之家其它相關文章!
相關文章
Python+tkinter實現(xiàn)一個繪圖風格控件
這篇文章主要為大家詳細介紹了Python如何利用tkinter實現(xiàn)一個簡單的繪圖風格控件,文中的示例代碼講解詳細,感興趣的小伙伴可以學習一下2023-09-09
使用Jest?在?Visual?Studio?Code?中進行單元測試的流程分析
Jest是一個流行的JavaScript測試框架,它提供了簡潔、靈活和強大的工具來編寫和運行單元測試,今天通過本文給大家介紹使用Jest在Visual Studio Code中進行單元測試的流程分析,感興趣的朋友跟隨小編一起看看吧2023-07-07

