Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiproccesing and error The process has forked and you cannot use this CoreFoundation functionality safely

I'm having this error when calling my function start and using the multiprocessing module on Python 2.7.8 . I'm using a mac OS 10.9.5.

The process has forked and you cannot use this CoreFoundation functionality safely. 
You MUST exec(). Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_
COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

Here is the code, under the classe Lattice. My function sansfin is working well and is returning a boolean, it only take as an argument self and an integer to loop on.

    def start(self):
        if __name__ == '__main__':
            self.run = True
            p = Process(target=self.sansfin, args=(1000,))
            p.start()
            p.join()

    def stop(self):
        self.run = False

I am quite lost with this message. I don't have found anything helpful there and elsewhere. Some are suggesting a bug ...

like image 526
InfiniteLooper Avatar asked Jun 05 '15 14:06

InfiniteLooper


1 Answers

To fix this error, you need to explicitly set the multiprocessing start method to spawn on MacOS. This can be achieved by adding the following immediately below if __name__ == '__main__'. For example:

import platform, multiprocessing
...
if __name__ == '__main__':
    if platform.system() == "Darwin":
        multiprocessing.set_start_method('spawn')

See Also:

  1. https://groups.google.com/forum/#!topic/psychopy-users/quulKzsQY-Y
  2. https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
  3. https://bugs.python.org/issue33725
like image 131
Michael Altfield Avatar answered Oct 30 '22 02:10

Michael Altfield