Monitoring the asyncio event loop
Question: I am writing an application using python3 and am trying out asyncio for the
first time. One issue I have encountered is that some of my coroutines block
the event loop for longer than I like. I am trying to find something along the
lines of top for the event loop that will show how much wall/cpu time is being
spent running each of my coroutines. If there isn't anything already existing
does anyone know of a way to add hooks to the event loop so that I can take
measurements?

I have tried using cProfile which gives some helpful output, but I am more
interested in time spent blocking the event loop, rather than total execution
time.


Answer: Event loop can already track if coroutines take much CPU time to execute. To
see it you should [enable debug
mode](https://docs.python.org/3/library/asyncio-
eventloop.html#asyncio.AbstractEventLoop.set_debug) with `set_debug` method:

    
    
    import asyncio
    import time
    
    
    async def main():
        time.sleep(1)  # Block event loop
    
    
    if __name__ == "__main__":
        loop = asyncio.get_event_loop()
        loop.set_debug(True)  # Enable debug
        loop.run_until_complete(main())
    

In output you'll see:

    
    
    Executing <Task finished coro=<main() [...]> took 1.016 seconds
    

By default it shows warnings for coroutines that blocks for more than 0.1 sec.
It's not documented, but based on asyncio [source
code](https://github.com/python/asyncio/blob/master/asyncio/base_events.py#L240),
looks like you can change `slow_callback_duration` attribute to modify this
value.

