简体   繁体   中英

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.

1 case) using Member variables of 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. However, when i executed it using python 3.7, a error occurred.(TypeError: Can't pickle weakref object)

2 case) using local variables

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.

Why can't execute multiprocess using member variables of 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.

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 . 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' ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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