简体   繁体   中英

How can I measure the length of an asyncio event loop?

I have an application which is calling loop.run_forever() after scheduling some tasks. Those tasks will connect to web services and then schedule new tasks on the loop based on input from those services.

I want to find a way of keeping track of the loop to check whether there are tasks being created which are never completing. Ideally I would measure the number of tasks in the loop periodically and write it to a file, or make it available via an http call.

您可以收集所有任务,然后对它们进行计数,计算“循环长度”的其他一些指标或执行检查。

asyncio.Task.all_tasks(loop=loop)

在 python 3.10 中,您可以像这样获取当前线程中的任务数:

len(asyncio.all_tasks(asyncio.get_running_loop()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM