python - zmq PUBhandler could not be used in __init__() method -
i have class mylogger
sending messages log server using pubhandler
.
an exception gets raised when mylogger
instanced in logworker.init()
method (like version 1), however, ok if mylogger
instanced in logworker.log_worker()
method (version 2).
any suggestions appreciated.
import logging multiprocessing import process import os import random import sys import time import zmq zmq.log.handlers import pubhandler class mylogger(object): '''''' def __init__(self, port, handler=none): self.port = port self.handler = handler or self._construct_sock_handler() self.logger = logging.getlogger() self.logger.setlevel(logging.info) if not self.logger.handlers: self.logger.addhandler(self.handler) def _construct_sock_handler(self): context = zmq.context() log_sock = context.socket(zmq.pub) log_sock.connect("tcp://127.0.0.1:%i" % self.port) time.sleep(0.1) handler = pubhandler(log_sock) return handler def get_logger(self): return self.logger def sub_logger(port, level=logging.debug): ctx = zmq.context() sub = ctx.socket(zmq.sub) sub.bind('tcp://127.0.0.1:%i' % port) sub.setsockopt(zmq.subscribe, "") logging.basicconfig(level=level) while true: level, message = sub.recv_multipart() if message.endswith('\n'): # trim trailing newline, appended again message = message[:-1] log = getattr(logging, level.lower()) log(message) class logworker(object): def __init__(self): - pass # version 1 + self.logger = mylogger(port).get_logger() # version 2 def log_worker(self, port): - self.logger = mylogger(port).get_logger() # version 1 print "starting logger @ %i level=%s" % (os.getpid(), logging.debug) while true: level = logging.info self.logger.log(level, "hello %i!" % os.getpid()) time.sleep(1) if __name__ == '__main__': if len(sys.argv) > 1: n = int(sys.argv[1]) else: n = 2 port = 5555 workers = [process(target=logworker().log_worker, args=(port,)) _ in range(n)] [w.start() w in workers] try: sub_logger(port) except keyboardinterrupt: pass finally: [ w.terminate() w in workers ]
answer pyzmq
owner minrk:
you cannot pass zmq contexts or sockets across fork boundary happens when instantiate subprocess multiprocessing. have make sure create context
after in subprocess.
solution:
def work(): worker = logworker(port) worker.log_worker() workers = [ process(target=work) _ in range(n) ]
Comments
Post a Comment