0
我希望能夠在進程啓動後創建新的multiprocessing.Value
或multiprocessing.Array
。就像這個例子:在進程啓動後聲明共享內存
# coding: utf-8
import multiprocessing
shared = {
'foo': multiprocessing.Value('i', 42),
}
def job(pipe):
while True:
shared_key = pipe.recv()
print(shared[shared_key].value)
process_read_pipe, process_write_pipe = multiprocessing.Pipe(duplex=False)
process = multiprocessing.Process(
target=job,
args=(process_read_pipe,)
)
process.start()
process_write_pipe.send('foo')
shared['bar'] = multiprocessing.Value('i', 24)
process_write_pipe.send('bar')
輸出繼電器:這裏
42
Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap
self.run()
File "/usr/lib/python3.5/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/home/bux/Projets/synergine2/p.py", line 12, in job
print(shared[shared_key].value)
KeyError: 'bar'
Process finished with exit code 0
問題是:shared
字典複製到process
時,它的啓動。但是,如果我在shared
字典中添加一個密鑰,進程將無法看到它。如何開始process
可以通知新的multiprocessing.Value('i', 24)
的存在?
它不能給予思想,因爲管:
同步的對象只能進程之間通過繼承
任何想法共享?
的答案是正確的,但我得到的性能問題:manager.dict()做工作,但數據醃製。進程不只是通知新的內存共享塊。 – bux