Search code examples
python-asynciometrics

how to measure python asyncio event loop metrics?


Is there a module to measure asyncio event loop metrics? or for asyncio event loop, what metrics we should monitor for performance analysis purpose?

e.g.

  • how many tasks in the event loop?
  • how many tasks in waiting states?

I'm not trying to measure the coroutine functions. aiomonitor has the functionality, but is not exactly what I need.


Solution

  • I hardly believe number of pending tasks or tasks summary will tell you much. Let's say you have 10000 tasks, 8000 of them pending: is it much, is it not? Who knows.

    Thing is - each asyncio task (or any other Python object) can consume different amount of different machine resources.

    Instead of trying to monitor asyncio specific objects I think it's better to monitor general metrics:

    • CPU usage
    • RAM usage
    • Network I/O (in case you're dealing with it)
    • Hard drive I/O (in case you're dealing with it)

    What comes to asyncio you should probably always use asyncio.Semaphore to limit max number of currently running jobs and implement a convenient way to change value of semaphore(s) (for example, through config file).

    It'll allow to alter workload on a concrete machine depending on its available and actually utilized resources.

    Upd:

    My question, will asyncio still accept new connections during this block?

    If your event loop is blocked by some CPU calculation, asyncio will start to process new connections later - when event loop is free again (if they're not time-outed at this moment).

    You should always avoid situation of freezing event loop. Freezed somewhere event loop means that all tasks everywhere in code are freezed also! Any kind of loop freezing breaks whole idea of using asynchronous approach regardless of number of tasks. Any kind of code, where event loop is freezed will have performance issues.

    As you noted, you can use ProcessPoolExecutor with run_in_executor to await for CPU-bound stuff, but you can use ThreadPoolExecutor to avoid freezing as well.