Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python子类化multiprocessing.Lock_Python_Python Multiprocessing - Fatal编程技术网

Python子类化multiprocessing.Lock

Python子类化multiprocessing.Lock,python,python-multiprocessing,Python,Python Multiprocessing,我试图理解为什么python不能编译下面的类 class SharedResource(multiprocessing.Lock): def __init__(self, blocking=True, timeout=-1): # super().__init__(blocking=True, timeout=-1) self.blocking = blocking self.timeout = timeout self.d

我试图理解为什么python不能编译下面的类

class SharedResource(multiprocessing.Lock):
    def __init__(self, blocking=True, timeout=-1):
        # super().__init__(blocking=True, timeout=-1)
        self.blocking = blocking
        self.timeout = timeout
        self.data = {}
TypeError:方法需要2个参数,得到3个

我之所以子类化Lock 我的目标是创建一个共享的资源列表,该列表一次只能在进程上使用

这一概念最终将出现在Flash应用程序中,在该应用程序中,请求不能同时使用资源

RuntimeError:锁定对象只能通过继承在进程之间共享

class SharedResource():
    def __init__(self, id, model):
        '''
        id: mode id
        model: Keras Model only one worker at a time can call predict
        '''  
        self.mutex = Lock()
        self.id = id
        self.model = model

manager = Manager()
shared_list = manager.list() # a List of models
shared_list.append(SharedResource())

def worker1(l):
    ...read some data
    while True:
        resource = l[0]
        with m:
            resource['model'].predict(...some data)
        time.sleep(60)  


if __name__ == "__main__":
   processes = [ Process(target=worker1, args=[shared_list])]
   for p in processes:
       p.start()
   for p in processes:
       p.join()

是给类定义带来了错误,还是在尝试创建对象时出现了错误?您可以显示创建此类实例的调用吗?
\uuuuu init\uuuu
没有任何文档化的公共参数(有一个
ctx
)--您正在考虑使用
acquire
方法--尽管您最好使用
Lock
成员而不是subclassing@AnthonySottile这是我第一次尝试,我添加了更多的细节来说明为什么在我的例子中这不起作用继承不涉及子类化——您不想子类化Lock。这意味着子进程可以从父进程继承锁。将在中讨论