Skip to content

Instantly share code, notes, and snippets.

@phg1024
Created July 10, 2014 21:39
Show Gist options
  • Select an option

  • Save phg1024/1fb2a32aecb1d60484ec to your computer and use it in GitHub Desktop.

Select an option

Save phg1024/1fb2a32aecb1d60484ec to your computer and use it in GitHub Desktop.
Simple multiprocessing example
from multiprocessing import Process, Queue
import random
import time
def f(i, q):
t = random.random() * 2
time.sleep(t)
print 'hello world', i
q.put((i, 'hello world from %d' % i))
if __name__ == '__main__':
q = Queue()
procs = []
for num in range(10):
p = Process(target=f, args=(num, q))
procs.append(p)
for p in procs:
p.start()
for p in procs:
p.join()
print 'Queue size:', q.qsize()
while not q.empty():
i, msg = q.get()
print i, msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment