# Measuring coroutine blocking time

**URL:** <https://discuss.python.org/t/measuring-coroutine-blocking-time/54399>\
**Category:** Async-SIG\
**Created:** [May 27, 2024, 6:10pm UTC](https://discuss.python.org/t/measuring-coroutine-blocking-time/54399 "2024-05-27T18:10:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![irgolic](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/irgolic/32/20133_2.png) [@irgolic](https://discuss.python.org/u/irgolic)\
**Post date:** [May 27, 2024, 6:10pm UTC](https://discuss.python.org/t/measuring-coroutine-blocking-time/54399/1 "2024-05-27T18:10:56Z")

</div>

I couldn’t find an easy way to measure how long an async coroutine blocks the event loop, so I rolled my own.

> <https://github.com/asynchronous-flows/asyncflows/blob/main/asyncflows/utils/async_utils.py#L154-L220>

I use this in conjunction with a lag monitor to keep track of event loop health and identify the next best targets for optimization.

It’s difficult to maintain: every so often I come across a library that does something new that I haven’t accounted for. It breaks across different python versions sometimes too.

I noticed that there are some coroutine time measurements in CPython.

> <https://github.com/python/cpython/blob/406ffb5293a8c9ca315bf63de1ee36a9b33f9aaf/Lib/asyncio/base_events.py#L1948-L2029>

Is there any interest in exposing a standard way to measure coroutine blocking time? Feels like something that could be part of the standard library.

---

<div class="post-metadata">

**Author:** ![achimnol](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/achimnol/32/28061_2.png) [@achimnol](https://discuss.python.org/u/achimnol)\
**Post date:** [May 28, 2024, 11:29am UTC](https://discuss.python.org/t/measuring-coroutine-blocking-time/54399/2 "2024-05-28T11:29:16Z")

</div>

Here are some discussions about this:

> [@Add asyncio monitoring hooks](https://discuss.python.org/t/add-asyncio-monitoring-hooks/26426):
>
> [Similar previous discussion](https://discuss.python.org/t/adding-monitoring-hooks-to-the-event-loop/3448) As an APM vendor (Datadog), I’d love to be able to provide deeper insights/metrics about the asyncio event loop for our customers. Some of the metrics I have been thinking about so far are: Over the lifetime of the process total number of tasks created/finished total number of context switches min/avg/p95/p99/max/etc blocking time per-task run Per-task total number of context switches total blocking vs idle time I’d also like to be able to associate the per…
