問題描述
我有這樣的場景:我創(chuàng)建了一個(gè)包含信號(hào)量的類元素的對(duì)象.
import multiprocessing as mpr類元素(對(duì)象):def __init__(self):self.sem = mpr.Semaphore()自我.xyz = 33定義樂趣(ch):a = ch.recv()打印(一個(gè)[0])打印(a[1].xyz)a[1].xyz = 99打印(a[1].xyz)el = 元素()( pa , ch ) = mpr.Pipe()proc = mpr.Process(target=fun, args=( ch, ) )proc.start()pa.send([你好", el ])打印(el.xyz)proc.join()
此代碼返回此錯(cuò)誤:
文件/usr/lib/python2.7/multiprocessing/forking.py",第 51 行,在 assert_spawning 中'通過繼承' % type(self).__name__RuntimeError:信號(hào)量對(duì)象只能通過繼承在進(jìn)程之間共享
但是如果我從 Element
的聲明中刪除信號(hào)量,代碼可以工作,但是分配給 a[1].xyz 的值將會(huì)丟失.
現(xiàn)在我需要通過信號(hào)量和多處理同步大量對(duì)象.那么有一些方法可以在每個(gè)對(duì)象中設(shè)置一個(gè)信號(hào)量并只傳遞對(duì)主對(duì)象的引用嗎?
import multiprocessing as mpr類元素(對(duì)象):def __init__(self):自我.xyz = 33定義樂趣(ch):a = ch.recv()打印(一個(gè)[0])打印(a[1].xyz)a[1].xyz = 99打印(a[1].xyz)el = 元素()( pa , ch ) = mpr.Pipe()proc = mpr.Process(target=fun, args=( ch, ) )proc.start()pa.send([你好", el ])打印(el.xyz)proc.join()
第二個(gè)版本不會(huì)產(chǎn)生任何錯(cuò)誤,但是分配給a[1].xyz = 99
的值會(huì)在主進(jìn)程中丟失.
我認(rèn)為您不了解 multiprocessing
模塊的工作原理.
當(dāng)您通過管道發(fā)送某些內(nèi)容時(shí),它會(huì)在子流程中被腌制然后解除腌制.這意味著子進(jìn)程實(shí)際上有一個(gè)原始對(duì)象的副本!這就是改變丟失"的原因.添加信號(hào)量不會(huì)改變?nèi)魏螙|西.
如果您想要共享內(nèi)存中的對(duì)象,您應(yīng)該使用 multiprocessing.Value,即使這不處理任意類型.可能 multiprocessing.Manager 是您正在尋找的.??p>
另一種方法是向提供修改對(duì)象的主進(jìn)程發(fā)送響應(yīng).
I've a scenario like this: I've created an object of the class element containing a semaphore.
import multiprocessing as mpr
class Element(object):
def __init__(self):
self.sem = mpr.Semaphore()
self.xyz = 33
def fun( ch ):
a = ch.recv()
print( a[0] )
print( a[1].xyz )
a[1].xyz = 99
print( a[1].xyz )
el = Element()
( pa , ch ) = mpr.Pipe()
proc = mpr.Process(target=fun , args=( ch, ) )
proc.start()
pa.send( [ "Hallo" , el ])
print( el.xyz )
proc.join()
This code return this error:
File "/usr/lib/python2.7/multiprocessing/forking.py", line 51, in assert_spawning
' through inheritance' % type(self).__name__
RuntimeError: Semaphore objects should only be shared between processes through inheritance
But if I remove the semaphore from the declaration of Element
the code works, but the value assigned to a[1].xyz will be lost.
Now I need to synchronizes a big collection of object via semphore and multiprocessing. So there's some method for setting a semaphore in every object and passing only the reference to the main object?
import multiprocessing as mpr
class Element(object):
def __init__(self):
self.xyz = 33
def fun( ch ):
a = ch.recv()
print( a[0] )
print( a[1].xyz )
a[1].xyz = 99
print( a[1].xyz )
el = Element()
( pa , ch ) = mpr.Pipe()
proc = mpr.Process(target=fun , args=( ch, ) )
proc.start()
pa.send( [ "Hallo" , el ])
print( el.xyz )
proc.join()
The second version dot't produce any error, but the value assigned to a[1].xyz = 99
will be lost in the main process.
I don't think you understood how the multiprocessing
module works.
When you send something through the pipe, it gets pickled and then unpickled in the subprocess. This means that the subprocess actually has a copy of the original object! That's why the change is "lost". Adding a semaphore wont change anything.
If you want an object in shared memory you should use multiprocessing.Value, even though this does not handle arbitrary types. Probably multiprocessing.Manager is what you are looking for.
An other way would be to send a response to the main process providing the modified object.
這篇關(guān)于Python多處理,傳遞包含信號(hào)量的對(duì)象引用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!