Reputation: 10204
I am playing with Python's multiprocessing module. I started with this program:
import multiprocessing
def worker(num):
"""thread worker function"""
print 'Worker:', num
return
if __name__ == '__main__':
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
p.start()
To my surprise, the results are always this:
Worker: 0
Worker: 1
Worker: 2
Worker: 3
Worker: 4
I would expect that the workers are output in a different order from time to time, but that never happened no matter how many times I run the program. Where did I misunderstand?
Upvotes: 0
Views: 213
Reputation: 31339
Your code is fine, its just very fast. Try this to add some jittering:
import multiprocessing
from random import randint
from time import sleep
def worker(num):
"""thread worker function"""
sleep(randint(0, 5))
print 'Worker:', num
return
if __name__ == '__main__':
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
p.start()
Upvotes: 1