作为标题,我想要一个日志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来解析server中的所有函数