gpt4 book ai didi

python - 使用多处理管理器共享对象(类实例)

转载 作者:太空狗 更新时间:2023-10-29 18:08:21 24 4
gpt4 key购买 nike

我需要在 python 中的多个进程之间共享一个对象及其方法。我正在尝试使用 managers(在模块 multiprocessing 中)但它崩溃了。这是一个愚蠢的生产者-消费者示例,其中两个进程之间的共享对象只是一个包含四个方法的数字列表。

from multiprocessing import Process, Condition, Lock  
from multiprocessing.managers import BaseManager
import time, os

lock = Lock()
waitC = Condition(lock)
waitP = Condition(lock)

class numeri(object):
def __init__(self):
self.nl = []

def getLen(self):
return len(self.nl)

def stampa(self):
print self.nl

def appendi(self, x):
self.nl.append(x)

def svuota(self):
for i in range(len(self.nl)):
del self.nl[0]

class numManager(BaseManager):
pass

numManager.register('numeri', numeri, exposed = ['getLen', 'appendi', 'svuota', 'stampa'])

def consume(waitC, waitP, listaNumeri):
lock.acquire()
if (listaNumeri.getLen() == 0):
waitC.wait()
listaNumeri.stampa()
listaNumeri.svuota()
waitP.notify()
lock.release()

def produce(waitC, waitP, listaNumeri):
lock.acquire()
if (listaNumeri.getLen() > 0):
waitP.wait()
for i in range(10):
listaNumeri.appendi(i)
waitC.notify()
lock.release()


def main():
mymanager = numManager()
mymanager.start()
listaNumeri = mymanager.numeri()
producer = Process(target = produce, args =(waitC, waitP, listaNumeri,))
producer.start()
time.sleep(2)
consumer = Process(target = consume, args =(waitC, waitP, listaNumeri,))
consumer.start()

main()

反正它总是那样崩溃,告诉我:

Process Process-3:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "./trySemProc.py", line 61, in consume
if (listaNumeri.getLen() == 0):
File "<string>", line 2, in getLen
File "/usr/lib/python2.7/multiprocessing/managers.py", line 755, in _callmethod
self._connect()
File "/usr/lib/python2.7/multiprocessing/managers.py", line 742, in _connect
conn = self._Client(self._token.address, authkey=self._authkey)
File "/usr/lib/python2.7/multiprocessing/connection.py", line 169, in Client
c = SocketClient(address)
File "/usr/lib/python2.7/multiprocessing/connection.py", line 293, in SocketClient
s.connect(address)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 2] No such file or directory

那到底是怎么回事呢?我应该如何使用这些管理器来共享对象及其方法?

最佳答案

你必须join您的进程以防止主进程在子进程继续执行之前退出。因此,将连接添加到您的代码中:

 consumer.join()
producer.join()

在调用进程的 start() 方法之后。

关于python - 使用多处理管理器共享对象(类实例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11951750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com