タイトルのとおり、新しいプロセスでログを処理するのに役立つログ rpc サーバーが必要です。
zerorpc とこのリンクを参照します: https://stackoverflow.com/a/57140017/14021161
しかし、奇妙なバグで立ち往生
コード:
import zerorpc
from loguru import logger
import time
import multiprocessing as mp
import collections
Job = collections.namedtuple('Job', ['event', 'args'])
class LogClient(mp.Process):
"""A process backed by an internal queue for simple one-way message passing.
"""
def __init__(self, ip='127.0.0.1', port='4242'):
super().__init__()
self.queue = mp.Queue()
self.c = zerorpc.Client()
address = f"tcp://{ip}:{port}"
self.c.connect(address)
self.start()
def put(self, event, *args):
"""Puts the event and args as a `Job` on the queue
"""
job = Job(event, args)
self.queue.put(job)
def _addLog(self, job):
event, args = job
self.c.addLog(*args)
def run(self):
while True:
job = self.queue.get()
self._addLog(job)
def addLog(self, level, *context):
self.put('addLog', level, *context)
lc = LogClient()
lc.c.addLog("WARNING", 'hello', 'Ray') # this work, but its witout multiprocess
lc.addLog("INFO", 'hello', 'Ray') # doesn't work
lc.terminate()
実際にジョブを受け取りますが、_addLog
実際にはサーバーに送信しません
アドバイスをいただければ幸いです。前もって感謝します。
| |
| |
ps 質問を明確にするために、私は のみを示してaddLog
いますが、実際にはサーバーは多くの IO ジョブを処理する必要があります。したがって、_addLog
func はdispatch
gettattr を使用して func になり、サーバー内のすべての関数を解析します。