Skip to content

Instantly share code, notes, and snippets.

@bear
Last active December 18, 2015 16:28
Show Gist options
  • Save bear/5811170 to your computer and use it in GitHub Desktop.
Save bear/5811170 to your computer and use it in GitHub Desktop.
off-the-cuff python multiprocessing sample
from Queue import Empty
from multiprocessing import Process, Queue
def handlerOne(inbound):
while True:
try:
key, item = inbound.get(False)
if item is not None:
print "do something"
except Empty:
time.sleep(0.1) # thread friendly
oneInbound = Queue()
procList = []
procList.append(Process(target=handlerOne, args=(oneInbound,)))
... as many as you need...
for p in procList:
p.start()
oneInbound.put("this will be handled")
... sometime later ...
for p in procList:
p.kill()
p.join() # causes the main thread to lock on p's status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment