简体   繁体   English

从__init __()启动类中的方法的多处理

[英]Start multiprocessing.Process of method in class, from __init__()

How can I start a multiprocessing.Process from within __init__() of a class, targeting another function in that class? 如何从一个类的__init__()内部启动multiprocessing.Process,以该类中的另一个函数为目标? The class itself shall not be a process. 班级本身不应是一个过程。 __init__() shall refer to a class variable assigned in the class, not inside any function. __init__()应该引用在类中分配的类变量,而不是在任何函数内部。

Working code: 工作代码:

import multiprocessing as mp
class SomeClass:
    def __init__(self):
        p_process1 = mp.Process(target=self.process1)
        p_process1.start()

    def process1(self):
        while True:
            pass

The code I want: 我想要的代码:

import multiprocessing as mp
class SomeClass:
    def __init__(self):
        self.p_process1.start()

    def process1(self):
        while True:
            pass
    p_process1 = mp.Process(target=process1)

If I now try to run the code I want, I get an error message: 如果现在尝试运行所需的代码,则会收到错误消息:

Process Process-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
TypeError: process1() missing 1 required positional argument: 'self'

Well the problem clearly states it: the multiprocessing calls it without parameters, and you however expect one: self . 好了,问题清楚地表明了这一点: multiprocessing 不带参数地调用它,但是您期望一个: self In this case you can solve it like: 在这种情况下,您可以像下面这样解决它:

import multiprocessing as mp

class SomeClass:
    def __init__(self):
        self.p_process1.start()

    @staticmethod
    def process1(): # so no parameter
        while True:
            pass
    p_process1 = mp.Process(target=process1)

If however you need a reference to self , there is no other option than to construct the method in a context where you have a reference to self . 但是如果您需要引用self ,则除了在引用self的上下文中构造方法之外,别无选择。 After all if you fetch self.process1 , you do not get a reference to SomeClass.process1 , you obtain a function that is equal to functools.partial(SomeClass.process1,self=self) , so you actually have a function where self is filled in implicitly. 毕竟,如果您获取self.process1则不会获得对SomeClass.process1的引用 ,您将获得一个等于functools.partial(SomeClass.process1,self=self)的函数,因此实际上您拥有一个self为隐式填充。

process1 is a bound function, it needs class instance as self to be the first arg when be called. process1是一个绑定函数,在调用时需要将类实例作为self作为第一个arg。

If you just want that arch, @property can help. 如果只需要该拱门, @property可以提供帮助。

import multiprocessing as mp
class SomeClass:
    def __init__(self):
        self.p_process1.start()

    def process1(self):
        while True:
            pass

    @property
    def p_process1(self):
        return mp.Process(target=self.process1)

SomeClass()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM