gpt4 book ai didi

Python 日志记录开销

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:53 27 4
gpt4 key购买 nike

我正在使用 asyncio 编写代码具有大量信息/调试日志记录的模块 (logging.FileHandler)。我担心在 asyncio 中大量使用日志记录会降低我的应用程序的性能,因为日志记录会阻止操作。

最好的解决方案是什么?没有找到有关日志记录开销的任何信息。

也许使用 SocketHandlerMemoryHandler会有帮助吗?对于我使用的指标 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

但这不是我要找的。

谢谢

最佳答案

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com