Search code examples
pythonpicklepython-multiprocessing

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?


Solution

  • 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.