Python Multiprocessing Error – may have been in progress in another thread when fork() was called

Multiprocessing Error – may have been in progress in another thread when fork() was called is an error which occurs because of added security to restrict multithreading.

This post is my attempt to explain to you why this error occurs and how you can solve it, I will also include multiple solutions that could be considered as alternative fixes to the error.

Exploring the Error : Multiprocessing Error – may have been in progress in another thread when fork() was called

First of all, the error occurs because of added security to restrict multithreading.

The error usually looks like the error bellow. Make sure yours is similar.

Python Multiprocessing Error - may have been in progress in another thread when fork() was called

Your error should be similar to the error above in order to avoid all kinds of confusion and mixing between errors and solutions.

Bellow is a number of tested solutions that I have tried and worked for me.

Solution 1 : use multiprocessing.get_context() and pool.map()

First of all, the first solution does not involve editing the file .bash_profile

The solution is rather simple, just add this to your code.

Python Multiprocessing Error - may have been in progress in another thread when fork() was called-1

In case this solution did not work, bellow you can find other possible solutions that might help you.

Solution 2 : edit the .bash_profile file.

The second solution which works for most people is to edit the file .bash_profile.

First run the following command.

Python Multiprocessing Error - may have been in progress in another thread when fork() was called-2

To the end of .bash_profile add the following line.

Python Multiprocessing Error - may have been in progress in another thread when fork() was called-3

Save the changes, If the error does not disappear the last chance is to use the solution bellow.

Solution 3 : edit your script environment.

The third solution is to set no_proxy = * in your script environment

You can read more about that in the link bellow.

https://github.com/python/cpython/issues/74570#issuecomment-1093748531

For many people, adding OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES solves the issue only when they set no_proxy = * in the script environment

This solution was the last one. Good luck.

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

This is one of those errors that are hard to explain or spot, sometimes the error may have been in progress in another thread when fork() was called comes with other problems with your code or Python which makes solving the issue even more complicated, I hope my article helped you solve this common issue, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/