Skip to content

Instantly share code, notes, and snippets.

@NoUsername
Created October 31, 2013 21:24
Show Gist options
  • Save NoUsername/7257470 to your computer and use it in GitHub Desktop.
Save NoUsername/7257470 to your computer and use it in GitHub Desktop.
from multiprocessing import Process, Queue
from Queue import Empty
MAX_MSG = 200000
NUM_PROC = 10
def startProducer(queue, out, max):
import time, random
time.sleep(2)
for i in range(max):
queue.put("hello %s"%i)
#time.sleep(0.01)
queue.put("quit")
out.put("producer_done")
def startConsumer(queue, out, name):
counter = 0
while True:
value = queue.get()
if value == "quit":
# put it back in the queue to kill others
queue.put("quit")
break
counter += 1
out.put("%s got value %s"%(name, value))
out.put("%s processed %s messages"%(name, counter))
if __name__=='__main__':
q = Queue()
out = Queue()
p = Process(target=startProducer, args=(q, out, MAX_MSG))
p.start()
for i in range(NUM_PROC):
p = Process(target=startConsumer, args=(q, out, "consumer%s"%i))
p.start()
print("all started")
try:
# long timeout for first read (for producer start delay)
val = out.get(True, 20)
while True:
print("val: %s"%val)
val = out.get(True, 2)
except Empty:
print("queue timeout")
except:
print("unknown error")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment