简体   繁体   English

如何测量异步事件循环的长度?

[英]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.

相关问题 如何学习如何实现自定义Python asyncio事件循环? - How can I learn how to implement a custom Python asyncio event loop? 如何处理异步嵌套函数中的“此事件循环已在运行”错误? - How can I deal with “This event loop is already running” error on a nested function in asyncio? 我是否需要跟踪 asyncio 事件循环,或者我可以在需要时调用 asyncio.get_event_loop 吗? - Do I need to keep track of the asyncio event loop, or can I just call asyncio.get_event_loop when it is needed? 如何在Python中为线程设置asyncio事件循环? - How do I set the asyncio event loop for a thread in Python? 如何将龙卷风 ioloop 设置为 asyncio event_loop? - How do I set the tornado ioloop to an asyncio event_loop? 如何使用 asyncio 遍历不确定长度的迭代器 - How to loop through an iterator of indeterminate length with asyncio 如何测量for循环的执行时间? - How can I measure the execution time of a for loop? (Python) 如何在带有累加器的 while 循环中应用 asyncio? - (Python) How can I apply asyncio in while loop with accumulator? 如何在主异步循环内的同步子进程中运行多个异步循环? - How can I run multiple asyncio loops inside syncrhonous sub-processes inside a main asyncio loop? 如何在没有名称的异步中停止无限事件循环? - How to stop an infinite event loop in asyncio with no name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM