当我在Python 3.5中启用asyncio调试时,我几乎每秒都会得到这样的消息:
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
这是一个重现此问题的最小代码:
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 # ...
如果sleep
延迟小于1秒,则不会打印消息,但如果> = 1,则不会打印消息.
这是预期的行为吗?我怎么能避免这样的消息?
我在使用Muffin框架时发现了这个问题.
您可以设置异步模块的日志记录级别,例如:
logging.getLogger('asyncio').setLevel(logging.WARNING) # Remove asyncio debug and info messages, but leave warnings.
顺便说一句,大致了解日志记录名称系统:
的
name
是一个潜在的时间段隔开的分层值,如foo.bar.baz
(但它也可以是只是简单foo
,例如)。层次结构列表中位于最下方的记录器是列表中较高处的记录器的子级。例如,给定一个记录器使用的名称foo
,记录仪用的名字foo.bar
,foo.bar.baz
以及foo.bam
是所有后代foo
。记录程序名称层次结构类似于Python包层次结构,如果您使用推荐的结构按模块对记录程序进行组织,则与之相同logging.getLogger(__name__)
。这是因为在模块中,__name__
是Python包命名空间中模块的名称。