[英]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.我有一个应用程序在安排一些任务后调用
loop.run_forever()
。 Those tasks will connect to web services and then schedule new tasks on the loop based on input from those services.这些任务将连接到 Web 服务,然后根据这些服务的输入在循环中安排新任务。
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.
理想情况下,我会定期测量循环中的任务数量并将其写入文件,或通过 http 调用使其可用。
您可以收集所有任务,然后对它们进行计数,计算“循环长度”的其他一些指标或执行检查。
asyncio.Task.all_tasks(loop=loop)
在 python 3.10 中,您可以像这样获取当前线程中的任务数:
len(asyncio.all_tasks(asyncio.get_running_loop()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.