简体   繁体   English

为什么不能使用 class 的成员变量执行多进程?

[英]Why can't execute multiprocess using member variables of class?

I tested a code using python 3.6 and python 3.7 by 2 cases.我使用 python 3.6 和 python 3.7 测试了 2 个案例的代码。

1 case) using Member variables of class 1例)使用class的成员变量

import multiprocessing as mp
import sys
class Foo:
    def do_multiprocessing(self):
        ctx = mp.get_context('spawn')
        self.process_1 = ctx.Process(target=self.do_stuff1)
        self.process_2 = ctx.Process(target=self.do_stuff2)

        self.process_1.start()
        self.process_2.start()

    def do_stuff1(self):
        print("Doing 1")

    def do_stuff2(self):
        print("Doing 2")

if __name__ == "__main__":
    print("PYTHON VERSION : ", sys.version)
    foo = Foo()
    foo.do_multiprocessing()

Result: When i executed it using python 3.6, working well.结果:当我使用 python 3.6 执行它时,运行良好。 However, when i executed it using python 3.7, a error occurred.(TypeError: Can't pickle weakref object)但是,当我使用 python 3.7 执行它时,发生了错误。(TypeError: Can't pickle weakref object)

2 case) using local variables 2 case) 使用局部变量

import multiprocessing as mp
import sys
class Foo:
    def do_multiprocessing(self):
        ctx = mp.get_context('spawn')
        process_1 = ctx.Process(target=self.do_stuff1)
        process_2 = ctx.Process(target=self.do_stuff2)

        process_1.start()
        process_2.start()

    def do_stuff1(self):
        print("Doing 1")

    def do_stuff2(self):
        print("Doing 2")

if __name__ == "__main__":
    print("PYTHON VERSION : ", sys.version)
    foo = Foo()
    foo.do_multiprocessing()

Result: When i executed it using python 3.6 and python 3.7, working well.结果:当我使用 python 3.6 和 python 3.7 执行它时,运行良好。

Why can't execute multiprocess using member variables of class?为什么不能使用 class 的成员变量执行多进程? And why can execute multiprocess using local variables?为什么可以使用局部变量执行多进程?

I can't understand why a code execute.我不明白为什么要执行代码。

ps) if using mp.get_context('fork'), the code works well using python 3.6 and python 3.7 on case 1. ps) 如果使用 mp.get_context('fork'),则代码在案例 1 中使用 python 3.6 和 python 3.7 运行良好。

By passing self.do_stuff2 to process_2 , you're trying to give that process access to the entirety of self (as an implicit argument), which includes self.process_1 .通过将self.do_stuff2传递给process_2 ,您试图让该进程访问整个self (作为隐式参数),其中包括self.process_1 You can see how giving one child process meaningful access to another might be complicated (unless you have the OS replicate the whole (parent) process with 'fork' ).您可以看到让一个子进程对另一个子进程进行有意义的访问可能很复杂(除非您让操作系统使用'fork'复制整个(父)进程)。

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

相关问题 将接受类成员函数作为变量的函数传递给python multiprocess pool.map() - Passing a function that accepts class member functions as variables into python multiprocess pool.map() 为什么Python Multiprocess类不更改属性? - Why Python Multiprocess class doesn't change the attribute? 为什么keras model不能在python的多进程池中运行? - why keras model can't run in multiprocess pool in python? 为什么multiprocess.Process不能调用getattr方法? - Why can't multiprocess.Process call getattr method? 为什么不能在类的成员函数中初始化QThread? - Why can't I initialize a QThread in a member function of class? Python-使用字符串列表作为类数据成员。 当字符串包含变量时,变量不会更新 - Python - Using list of strings as class data member. When strings contain variables, variables don't update 使用元类自动分配类的成员变量 - Using metaclass to automatically assign member variables of a class 为什么不能在__init__关键字arg中使用类变量? - Why can't class variables be used in __init__ keyword arg? 为什么@ var.setter函数无法访问类变量? - Why can't @var.setter function access class variables? 为什么在这种多进程情况下队列不能用于通信 - why the queue can not works for communication in this multiprocess case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM