Python 将锁定与“pathos.pools.ProcessPool”结合使用的标准方法是什么? 我们考虑下面的例子: from pathos.pools import ProcessPool class A: def run(self, arg: int): shared_variable = 100 def __run_parallel(arg: int): local_variable = 0 # ... if local_variable > shared_variable: shared_variable = local_variable ProcessPool(4).map(__run_parallel, range(1000))
很明显,当使用四个进程时,Python 将锁定与“pathos.pools.ProcessPool”结合使用的标准方法是什么? 我们考虑下面的例子: from pathos.pools import ProcessPool class A: def run(self, arg: int): shared_variable = 100 def __run_parallel(arg: int): local_variable = 0 # ... if local_variable > shared_variable: shared_variable = local_variable ProcessPool(4).map(__run_parallel, range(1000)),python,python-3.x,pathos,Python,Python 3.x,Pathos,很明显,当使用四个进程时,if local\u variable>shared\u variable:与shared\u variable=local\u variable之间存在数据竞争 因此,我想在if块周围引入一种锁定机制,因此我尝试了以下方法: from pathos.pools import ProcessPool from multiprocessing import Lock class A: def run(self, arg: int): lock
if local\u variable>shared\u variable:
与shared\u variable=local\u variable
之间存在数据竞争
因此,我想在if
块周围引入一种锁定机制,因此我尝试了以下方法:
from pathos.pools import ProcessPool
from multiprocessing import Lock
class A:
def run(self, arg: int):
lock = Lock()
shared_variable = 100
def __run_parallel(arg: int):
local_variable = 0
# ...
lock.acquire()
if local_variable > shared_variable:
shared_variable = local_variable
lock.release()
ProcessPool(4).map(__run_parallel, range(1000))
但是,我得到一个错误RuntimeError:Lock对象只能通过继承在进程之间共享
在多处理
库中,实现所需互斥的标准方法似乎是使用管理器
对象
然而,如何在pathos
中惯用地做到这一点?pathos
利用了多进程
,它与多进程
具有相同的接口,但使用了dill
。您可以通过以下任一方式访问它
>>> import pathos as pa
>>> import multiprocess as mp
>>> mp.Manager is pa.helpers.mp.Manager
True