我有一個自定義對象的大型數組,我需要執行獨立(可並行)的任務,包括修改對象參數。我試過使用管理器().dict和'sharedmem'ory,但都沒有工作。例如:在python多處理中修改對象
import numpy as np
import multiprocessing as mp
import sharedmem as shm
class Tester:
num = 0.0
name = 'none'
def __init__(self,tnum=num, tname=name):
self.num = tnum
self.name = tname
def __str__(self):
return '%f %s' % (self.num, self.name)
def mod(test, nn):
test.num = np.random.randn()
test.name = nn
if __name__ == '__main__':
num = 10
tests = np.empty(num, dtype=object)
for it in range(num):
tests[it] = Tester(tnum=it*1.0)
sh_tests = shm.empty(num, dtype=object)
for it in range(num):
sh_tests[it] = tests[it]
print sh_tests[it]
print '\n'
workers = [ mp.Process(target=mod, args=(test, 'some')) for test in sh_tests ]
for work in workers: work.start()
for work in workers: work.join()
for test in sh_tests: print test
打印出:
0.000000 none
1.000000 none
2.000000 none
3.000000 none
4.000000 none
5.000000 none
6.000000 none
7.000000 none
8.000000 none
9.000000 none
0.000000 none
1.000000 none
2.000000 none
3.000000 none
4.000000 none
5.000000 none
6.000000 none
7.000000 none
8.000000 none
9.000000 none
即對象沒有被修改。
我該如何達到理想的行爲?
的http://計算器。com/questions/10721915/shared-memory-objects-in-python-multiprocessing – tacaswell 2013-04-07 03:24:52
你能發佈一個鏈接到'sharedmem'我似乎無法找到任何東西。 – tacaswell 2013-04-07 03:25:12