简体   繁体   中英

Python Multiprocessing Pickling Confusion in Classes

I have been told that pickling class methods for multiprocessing are not possible with Processes. For some reason though, this code works. I have tried to complete this same task in other applications and my results have been inconsistent. What can I do for this feature to consistently work?

from multiprocessing import Process

class test():
  def run(self):
    print("HI")

  def p(self):
    p = Process(target=self.run)
    p.start()


a = test()
a.p()

I have tried utilizing this functionality with python3 and python and it has worked and not worked for both versions. How do I ensure this works?

When starting a new process, Windows and Mac Spawn the process which requires objects to be pickled. On Linux however, the process is Forked which inherits memory from the parent process. If a class contains non-pickle-able objects and you want to run a class method on a separate process, you must use Linux.

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