Test.py
from multiprocessing import Process
from random import random
class Test:
def __init__(self):
self.val= 0.0
def func(self):
self.val=random()
print self.val
if __name__ == '__main__':
tests=[]
prcs= []
for i in range(2):
tests.append(Test())
prcs.append( Process(target=tests[i].func) )
for p in prcs:
p.start()
for p in prcs:
p.join()
for t in tests:
print t.val
When you run such a program,
$ python Test.py
0.577127051322
0.331891187398
0.0
0.0
The result is. In other words, the value stored in self.val
inside the functionfunc ()
is invalid after that.
On the other hand, add ()
to the function passed to Process
, that is,
prcs.append( Process(target=tests[i].func()) )
If you change it like
$ python Test.py
0.872880584458
0.255770370005
0.872880584458
0.255770370005
The self.val
value assigned in the function func
is stored firmly as shown in.
I don't understand what this means. .. ..
Recommended Posts