我正在使用 asyncio 编写代码具有大量信息/调试日志记录的模块 (logging.FileHandler)。我担心在 asyncio 中大量使用日志记录会降低我的应用程序的性能,因为日志记录会阻止操作。
最好的解决方案是什么?没有找到有关日志记录开销的任何信息。
也许使用 SocketHandler或 MemoryHandler会有帮助吗?对于我使用的指标 statsd (这也是阻塞操作,但必须非常快),但我更关心非阻塞日志记录。
代码示例:
@asyncio.coroutine
def creator_worker(self):
while not self.q.empty():
with (yield from self.semaphore):
sample = yield from self.q.get()
logging.debug('Got new sample, processing')
# start processing
我知道
import os
os.environ['PYTHONASYNCIODEBUG'] = '1'
import asyncio
但这不是我要找的。p>
谢谢
最佳答案
Logging 文档提供了一种解决方案,通过一个QueueHandler 进行日志记录,然后在另一个线程中使用一个QueueListener 来处理日志记录。 https://docs.python.org/3.6/howto/logging-cookbook.html#dealing-with-handlers-that-block
关于Python 日志记录开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35698025/