Created
July 10, 2014 21:39
-
-
Save phg1024/1fb2a32aecb1d60484ec to your computer and use it in GitHub Desktop.
Simple multiprocessing example
This file contains hidden or 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 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