我如何在 Python 进程之间共享一个嵌套对象,对 tasklet(协程)进行写访问?
这是一个简化的示例,其中包含我为正确提出这个问题而写的类比;
首先请安装greenlet包:sudo pip install greenlet
在下面的例子中:
habitat变量引用的Nature类实例Nature类的这个实例有一个名为animals的实例变量- 在启动
Nature类实例的同时,创建了 8 个不同的Animal实例并将其附加到animals实例变量。现在,如果我是正确的,这个Nature实例是一个嵌套对象。 - 作为最后一步
liveAnimal实例函数使用greenlet包的switch()函数随机切换直到global_counter达到 1000。此live函数随机更改Animal实例的limbs实例变量的值。
greentest.py:
import random
from greenlet import greenlet
global_counter = 0
class Animal():
def __init__(self,nature):
self.limbs = 0
nature.animals.append(self)
self.tasklet = greenlet(self.live)
def live(self,nature):
global global_counter
while True:
self.limbs = random.randint(1, 10)
global_counter += 1
if global_counter > 1000:
break
random.sample(nature.animals,1)[0].tasklet.switch(nature)
class Nature():
def __init__(self,how_many):
self.animals = []
for i in range(how_many):
Animal(self)
print str(how_many) + " animals created."
self.animals[0].live(self)
结果是:
>>> import greentest
>>> habitat = greentest.Nature(8)
8 animals created.
>>> habitat.animals[0].limbs
3
>>> greentest.global_counter
1002
按预期工作。更改 limbs 和 global_counter 的值(非零)
但是当我将 multiprocessing 添加到等式时;
greentest2.py:
import random
import multiprocessing
from greenlet import greenlet
global_counter = 0
class Animal():
def __init__(self,nature):
self.limbs = 0
nature.animals.append(self)
self.tasklet = greenlet(self.live)
def live(self,nature):
global global_counter
while True:
self.limbs = random.randint(1, 10)
global_counter += 1
if global_counter > 1000:
break
random.sample(nature.animals,1)[0].tasklet.switch(nature)
class Nature():
def __init__(self,how_many):
self.animals = []
for i in range(how_many):
Animal(self)
print str(how_many) + " animals created."
#self.animals[0].live(self)
jobs = []
for i in range(2):
p = multiprocessing.Process(target=self.animals[0].live, args=(self,))
jobs.append(p)
p.start()
结果不如预期:
>>> import greentest2
>>> habitat = greentest2.Nature(8)
8 animals created.
>>> habitat.animals[0].limbs
0
>>> greentest2.global_counter
0
limbs 和 global_counter 的值都没有改变(零)。我认为这是因为 Animal 类和 global_counter 的实例不在进程之间共享。那么如何在进程之间共享这个 Nature 类实例或这些 Animal 类实例呢?
ADDITION 根据@noxdafox 的回答;
greentest3.py:
import random
import multiprocessing
from greenlet import greenlet
global_counter = multiprocessing.Value('i', 0)
class Animal():
def __init__(self,nature):
self.limbs = 0
nature.animals.append(self)
self.tasklet = greenlet(self.live)
def live(self,nature):
global global_counter
while True:
self.limbs = random.randint(1, 10)
global_counter.value += 1
if global_counter.value > 1000:
break
random.sample(nature.animals,1)[0].tasklet.switch(nature)
class Nature():
def __init__(self,how_many):
self.animals = []
for i in range(how_many):
Animal(self)
print str(how_many) + " animals created."
#self.animals[0].live(self)
jobs = []
for i in range(2):
p = multiprocessing.Process(target=self.animals[0].live, args=(self,))
jobs.append(p)
p.start()
然后结果是:
>>> import greentest3
>>> habitat = greentest3.Nature(8)
8 animals created.
>>> habitat.animals[0].limbs
0
>>> greentest3.global_counter.value
1004
我非常清楚 global_counter 可以与此方法共享,因为它是一个整数,但我实际上是在问如何共享 Nature< 的实例 和 Animal 进程之间的类。
请您参考如下方法:
不同进程不共享内存。
如果您需要共享的是单个变量,您可能可以使用 multiprocessing.Value
import multiprocessing
def function(counter):
counter.value += 1
counter = multiprocessing.Value('i')
p = multiprocessing.Process(target=function, args=(counter))
p.start()
p.join()
编辑:根据更新回答。
没有允许在内存中共享整个对象的抽象机制。共享内存通常实现为一个简单的数组,一旦获得资源,进程就可以在其中读/写。
此外,OOP 和线程/多处理不能很好地结合在一起。恕我直言,应将其视为反模式。在复杂对象之上,您可以添加对其属性的并发访问和修改。这是漫长而乏味的调试 session 的单程票。
推荐的模式是使用消息队列。将线程和进程想象成通过特定 channel 进行通信的独立实体可以显着简化问题。
