Created
June 1, 2012 21:38
-
-
Save lebedov/2855300 to your computer and use it in GitHub Desktop.
Demo of how to use the logging module with pyzmq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Demo of how to use the logging module with pyzmq. | |
""" | |
import logging | |
import zmq | |
import multiprocessing as mp | |
def sub(): | |
ctx = zmq.Context() | |
logger = logging.getLogger('sub') | |
# Set up data communication port: | |
subscriber = ctx.socket(zmq.SUB) | |
subscriber.connect('ipc://data') | |
subscriber.setsockopt(zmq.SUBSCRIBE, '') | |
# Set up synchronization port: | |
sync = ctx.socket(zmq.REQ) | |
sync.connect('ipc://sync') | |
# Synchronize: | |
sync.send('') | |
sync.recv() | |
# Process data: | |
while True: | |
data = subscriber.recv() | |
if data == 'quit': | |
break | |
logger.info('received: %s' % data) | |
def pub(): | |
ctx = zmq.Context() | |
logger = logging.getLogger('pub') | |
# Set up data communication port: | |
publisher = ctx.socket(zmq.PUB) | |
publisher.bind('ipc://data') | |
# Set up synchronization port: | |
sync = ctx.socket(zmq.REP) | |
sync.bind('ipc://sync') | |
# Synchronize: | |
data = sync.recv() | |
sync.send('') | |
# Send data: | |
for i in range(10): | |
data = str(i) | |
publisher.send(data) | |
logger.info('sent: %s' % data) | |
# Quit: | |
publisher.send('quit') | |
if __name__ == '__main__': | |
logging.basicConfig(level=logging.DEBUG, | |
format='%(asctime)s %(name)s %(levelname)s %(message)s') | |
mp.Process(target=sub).start() | |
pub() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment