diff options
author | Dan Klein <danklei@cisco.com> | 2015-08-24 17:28:17 +0300 |
---|---|---|
committer | Dan Klein <danklei@cisco.com> | 2015-08-24 17:28:17 +0300 |
commit | 7d3be8c612e295820649779335288c197b80ccb2 (patch) | |
tree | 78e9636bc8780dedc919c30378a621f425e1cbfc /external_libs/python/pyzmq-14.7.0/examples/logger/zmqlogger.py | |
parent | dab741a80699f86e86c91718872a052cca9bbb25 (diff) |
Changes location of console and fixed dependencies
Diffstat (limited to 'external_libs/python/pyzmq-14.7.0/examples/logger/zmqlogger.py')
-rw-r--r-- | external_libs/python/pyzmq-14.7.0/examples/logger/zmqlogger.py | 70 |
1 files changed, 0 insertions, 70 deletions
diff --git a/external_libs/python/pyzmq-14.7.0/examples/logger/zmqlogger.py b/external_libs/python/pyzmq-14.7.0/examples/logger/zmqlogger.py deleted file mode 100644 index c55b51b3..00000000 --- a/external_libs/python/pyzmq-14.7.0/examples/logger/zmqlogger.py +++ /dev/null @@ -1,70 +0,0 @@ -""" -Simple example of using zmq log handlers - -This starts a number of subprocesses with PUBHandlers that generate -log messages at a regular interval. The main process has a SUB socket, -which aggregates and logs all of the messages to the root logger. -""" - -import logging -from multiprocessing import Process -import os -import random -import sys -import time - -import zmq -from zmq.log.handlers import PUBHandler - -LOG_LEVELS = (logging.DEBUG, logging.INFO, logging.WARN, logging.ERROR, logging.CRITICAL) - -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, which will get appended again - message = message[:-1] - log = getattr(logging, level.lower()) - log(message) - -def log_worker(port, interval=1, level=logging.DEBUG): - ctx = zmq.Context() - pub = ctx.socket(zmq.PUB) - pub.connect('tcp://127.0.0.1:%i' % port) - - logger = logging.getLogger(str(os.getpid())) - logger.setLevel(level) - handler = PUBHandler(pub) - logger.addHandler(handler) - print "starting logger at %i with level=%s" % (os.getpid(), level) - - while True: - level = random.choice(LOG_LEVELS) - logger.log(level, "Hello from %i!" % os.getpid()) - time.sleep(interval) - -if __name__ == '__main__': - if len(sys.argv) > 1: - n = int(sys.argv[1]) - else: - n = 2 - - port = 5555 - - # start the log generators - workers = [ Process(target=log_worker, args=(port,), kwargs=dict(level=random.choice(LOG_LEVELS))) for i in range(n) ] - [ w.start() for w in workers ] - - # start the log watcher - try: - sub_logger(port) - except KeyboardInterrupt: - pass - finally: - [ w.terminate() for w in workers ] |