Skip to content

Instantly share code, notes, and snippets.

@sleepynate
Created March 16, 2012 03:46
Show Gist options
  • Save sleepynate/2048430 to your computer and use it in GitHub Desktop.
Save sleepynate/2048430 to your computer and use it in GitHub Desktop.
Multiprocessing Example.
import time, random
from multiprocessing import Process, Pipe, current_process
from multiprocessing.connection import wait
def foo(w):
for i in range(10):
w.send((i, current_process().name))
w.close()
if __name__ == '__main__':
readers = []
for i in range(4):
r, w = Pipe(duplex=False)
readers.append(r)
p = Process(target=foo, args=(w,))
p.start()
# We close the writable end of the pipe now to be sure that
# p is the only process which owns a handle for it. This
# ensures that when p closes its handle for the writable end,
# wait() will promptly report the readable end as being ready.
w.close()
while readers:
for r in wait(readers):
try:
msg = r.recv()
except EOFError:
readers.remove(r)
else:
print(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment