Last active
December 18, 2015 16:28
-
-
Save bear/5811170 to your computer and use it in GitHub Desktop.
off-the-cuff python multiprocessing sample
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
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