Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asyncio in python3.5 spams with INFO

When I enable asyncio debugging in Python 3.5, I get messages like this almost every second:

INFO:asyncio:poll 999.470 ms took 1001.159 ms: timeout
INFO:asyncio:poll 999.264 ms took 1001.092 ms: timeout
INFO:asyncio:poll 999.454 ms took 1001.059 ms: timeout

Here is a minimal code which reproduces this problem:

import asyncio
import logging
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop()
loop.set_debug(True)

async def f(delay):
    for i in range(10):
        await asyncio.sleep(delay)

loop.run_until_complete(f(0.5))
# (nothing printed)
loop.run_until_complete(f(1))
# INFO:asyncio:poll 999.470 ms took 1001.159 ms: timeout
# INFO:asyncio:poll 999.264 ms took 1001.092 ms: timeout
# INFO:asyncio:poll 999.454 ms took 1001.059 ms: timeout
# ...
loop.run_until_complete(f(2))
# INFO:asyncio:poll 1999.427 ms took 2001.112 ms: timeout
# INFO:asyncio:poll 1999.393 ms took 2001.088 ms: timeout
# INFO:asyncio:poll 1999.709 ms took 2001.758 ms: timeout
# ...

This doesn't print messages if sleep delay is lesser than 1 second, but does if it is >= 1.

Is that expected behaviour? How can I avoid such messages?

I found that problem when working with Muffin framework.

like image 683
MarSoft Avatar asked Dec 28 '15 22:12

MarSoft


2 Answers

You can set logging level for asyncio module, for example:

logging.getLogger('asyncio').setLevel(logging.WARNING)  # Remove asyncio debug and info messages, but leave warnings.

BTW, read about logging names system generally:

The name is potentially a period-separated hierarchical value, like foo.bar.baz (though it could also be just plain foo, for example). Loggers that are further down in the hierarchical list are children of loggers higher up in the list. For example, given a logger with a name of foo, loggers with names of foo.bar, foo.bar.baz, and foo.bam are all descendants of foo. The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(__name__). That’s because in a module, __name__ is the module’s name in the Python package namespace.

like image 66
Mikhail Gerasimov Avatar answered Nov 15 '22 17:11

Mikhail Gerasimov


Is that expected behaviour?

Yes, see https://github.com/python/cpython/blob/v3.6.1/Lib/asyncio/base_events.py#L1372

How can I avoid such messages?

Obviously, one way is to disable logging all-together. Or you could only print logging.DEBUG level. Or you could write some sort of custom filter to filter out these messages.

like image 45
Jashandeep Sohi Avatar answered Nov 15 '22 17:11

Jashandeep Sohi