Queue 和 Process 子类实例化行为
Queue and Process subclass instantiation behavior
我是 python 多进程 API 的新手。我有一个 multiprocess.Process()
的自定义子类,我们称它为 MyProcess
。我看到很多例子在 __main__
中定义 Queues
然后传递给 Process
构造函数。
在我的例子中,我生成了 N Process
个子类和每个子类 2 个 Queue
(pre 和 post 进程)。我更愿意将 Queue
初始化放在每个子进程中:
import multiprocessing as mp
class MyProcess(mp.Process) :
def __init__(self,ID) :
mp.Process.__init__(self)
self.name = ID
self.queues = {'pre':mp.Queue(),'post':mp.Queue()}
if __name__ == "__main__" :
my_proc = MyProcess(ID)
而不是:
import multiprocessing as mp
class MyProcess(mp.Process) :
def __init__(self,ID,queues) :
mp.Process.__init__(self)
self.name = ID
self.queues = queues
if __name__ == "__main__" :
my_proc = MyProcess(ID,{'pre':mp.Queue(),'post':mp.Queue()})
这可能吗?这里有 pickle/sync/scope 问题吗?
经过一些测试,后者似乎工作正常。
我是 python 多进程 API 的新手。我有一个 multiprocess.Process()
的自定义子类,我们称它为 MyProcess
。我看到很多例子在 __main__
中定义 Queues
然后传递给 Process
构造函数。
在我的例子中,我生成了 N Process
个子类和每个子类 2 个 Queue
(pre 和 post 进程)。我更愿意将 Queue
初始化放在每个子进程中:
import multiprocessing as mp
class MyProcess(mp.Process) :
def __init__(self,ID) :
mp.Process.__init__(self)
self.name = ID
self.queues = {'pre':mp.Queue(),'post':mp.Queue()}
if __name__ == "__main__" :
my_proc = MyProcess(ID)
而不是:
import multiprocessing as mp
class MyProcess(mp.Process) :
def __init__(self,ID,queues) :
mp.Process.__init__(self)
self.name = ID
self.queues = queues
if __name__ == "__main__" :
my_proc = MyProcess(ID,{'pre':mp.Queue(),'post':mp.Queue()})
这可能吗?这里有 pickle/sync/scope 问题吗?
经过一些测试,后者似乎工作正常。