异步多进程日志记录
logbook-aiopipe的Python项目详细描述
logbook_aioppe—异步多进程日志记录
此包为多进程提供处理程序和订阅服务器
^{
示例
下面的示例显示了多进程日志记录的典型应用程序。结果是
两条日志消息,hello from parent process
和hello from child process
,是
按顺序打印。
fromcontextlibimportclosingfrommultiprocessingimportProcessimportasynciofromaiopipeimportaiopipefromlogbook_aiopipeimportAioPipeSubscriber, \ AioPipeHandlerfromlogbookimportLogger,StderrHandlerasyncdefmainTask(eventLoop):# The parent process logger can be set up as normal.log=Logger()log.handlers.append(StderrHandler())rx,tx=aiopipe()sub=AioPipeSubscriber(awaitrx.open(eventLoop),log)withclosing(sub):subTask=eventLoop.create_task(sub.run())withtx.send()astx:proc=Process(target=childProc,args=(tx,))proc.start()log.info("hello from parent process")proc.join()awaitsubTaskdefchildProc(tx):eventLoop=asyncio.new_event_loop()eventLoop.run_until_complete(childTask(eventLoop,tx))asyncdefchildTask(eventLoop,tx):log=Logger()# The child process should use only `AioPipeHandler` as# its handler.handler=AioPipeHandler(awaittx.open(eventLoop))log.handlers.append(handler)withclosing(handler):log.info("hello from child process")eventLoop=asyncio.get_event_loop()eventLoop.run_until_complete(mainTask(eventLoop))
安装
此软件包要求python>;=3.5.0,可以使用pip
:
pip install logbook_aiopipe