How to run through an array with multiple processes (for example 2) in parallel? For example I have this code:
from multiprocessing import Process, Value import time, os n=Value('i', 0) l=[1, 2, 3, 4, 5] def fun(): try: while True: #time.sleep(0.01) not used, explanation below print os.getpid(), l[n.value] n.value+=1 except: return for i in range(2): p=Process(target=fun) p.start() Expected output should be like:
111 1 112 2 111 3 112 4 111 5 But I'm getting:
111 1 111 2 111 3 111 4 111 5 Only one process is running through an array.
I'm getting expected result only by putting time.sleep() with any very small value, but I'm looking for solution without it, if possible.