Search code examples
pythonpython-3.xpython-asyncio

How to increase the threshold time of the Python asyncio loop warning?


We have an asyncio based python 3.11.3 program that needs to do a computation such as scipy fft once in a while, which causes the event loop to print warnings like the one below. We presume that this warning is triggered when an event loop invocation takes longer than some predefined time period.

Is there a to control that time? We would like to keep the warnings enabled but increase the threshold time to let's say 0.2 sec. Or alternatively, disable the warnings just for operations which we expect take a longer time.

0152289 WARNING base_events.py: Executing <Task pending name='rx_task_03' coro=<SerialPacketsClient.__loop_runner_task() running at C:\Users\user\AppData\Roaming\Python\Python311\site-packages\serial_packets\client.py:196> wait_for=<Future pending cb=[Task.task_wakeup()] created at C:\Program Files\Python311\Lib\asyncio\base_events.py:427> created at C:\Program Files\Python311\Lib\asyncio\tasks.py:374> took 0.156 seconds

Solution

  • You can configure the threshold by setting the slow_callback_duration attribute from the loop:

    loop.slow_callback_duration = 0.2 #seconds
    

    But you should consider running these tasks using an executor to run them on a separate thread or process. In larger projects my usual approach is to have a shared Process Pool for running all those CPU-bound tasks. Check out this documentation:

    https://docs.python.org/3/library/asyncio-eventloop.html#executing-code-in-thread-or-process-pools