Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demo of how to use the logging module with pyzmq
#!/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
You can’t perform that action at this time.