python multiprocess update dictionary synchronously
Posted
by
user1050325
on Stack Overflow
See other posts from Stack Overflow
or by user1050325
Published on 2012-09-21T21:35:38Z
Indexed on
2012/09/21
21:37 UTC
Read the original article
Hit count: 264
I am trying to update one common dictionary through multiple processes. Could you please help me find out what is the problem with this code? I get the following output:
inside function
{1: 1, 2: -1}
comes here
inside function
{1: 0, 2: 2}
comes here
{1: 0, 2: -1}
Thanks.
from multiprocessing import Lock, Process, Manager
l= Lock()
def computeCopyNum(test,val):
l.acquire()
test[val]=val
print "inside function"
print test
l.release()
return
a=dict({1: 0, 2: -1})
procs=list()
for i in range(1,3):
p = Process(target=computeCopyNum, args=(a,i))
procs.append(p)
p.start()
for p in procs:
p.join()
print "comes here"
print a
© Stack Overflow or respective owner