我使用BaseManager共享队列;和queuelistener用于记录。这在linux上工作正常,但在以下代码的窗口中显示“获取服务器”消息,但不显示“获取服务器”日志消息。中间唯一的一行是对BaseManager.get_server()的调用。为什么?可以修复吗?
def server(eventq, logq):
""" publish event queue """
log = logging.getLogger()
log.handlers = [QueueHandler(logq)]
BaseManager.register('get_eventq', callable=lambda: eventq)
m = BaseManager(address=('', config["port"]), authkey=config["authkey"])
log.info("getting server")
try:
s = m.get_server()
except:
log.exception()
log.info("got server")
s.serve_forever()
def listener(eventq, logq):
""" listener for event queue """
log = logging.getLogger()
log.handlers = [QueueHandler(logq)]
log.info("starting listener")
while True:
p = Process(target=callback, args=(eventq.get(),))
p.start()
def main():
""" setup queue and listen for events
"""
# logging queue
log = logging.getLogger()
logq = Queue()
l = QueueListener(logq, *log.handlers)
l.start()
log.handlers = [QueueHandler(logq)]
# server for event queue
eventq = Queue()
p = Process(target=server, args=(eventq, logq))
p.start()
# listener
log.info("starting listener process")
p = Process(target=listener, args=(eventq, logq))
p.start()
p.join()
答案 0 :(得分:0)
我不确定您到底出错的地方(并发可能很棘手),但您正在做一些日志文档不推荐的事情。例如,您不应直接分配给log.handlers
,而应使用addHandler
API。请记住,用于多处理的Windows和Linux模型是不同的 - Linux使用分叉,而Windows则不然。我不认为您的问题与日志记录有关,但更多与底层多进程实例化有关。食谱示例有an example of multiprocessing with logging,它使用队列,您可以根据自己的需要进行调整。