Skip to content

Instantly share code, notes, and snippets.

@saghul
Created June 25, 2011 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saghul/1046942 to your computer and use it in GitHub Desktop.
Save saghul/1046942 to your computer and use it in GitHub Desktop.
ZeroMQ multithreaded producer example
# producer
import os
import random
import threading
import time
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.setsockopt(zmq.LINGER, 0) # discard unsent messages on close
socket.connect('tcp://127.0.0.1:5000')
stop_event = threading.Event()
def run():
while not stop_event.is_set():
msg = 'test Hello from process %s and thread %s' % (os.getpid(), threading.current_thread())
socket.send(msg)
time.sleep(random.randint(1, 5))
threads = []
for i in xrange(1, 11):
t = threading.Thread(target=run)
t.start()
threads.append(t)
while True:
msg = raw_input('> ')
if msg == 'quit':
break
stop_event.set()
[t.join() for t in threads]
socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment