Where to put freeze_support () in a Python script?

I am confused about using freeze_support() for multiprocessing and I get a Runtime Error without it. I just run the script, not define a function or module. Can I still use this? Or packages that I import should use this?

Here is the documentation.

Note that the specific scikit-learn problem is by calling GridSearchCV scikit-learn GridSearchCV which is trying to spawn processes in parallel. I'm not sure if my script should be frozen for this, or some kind of code that called (from the Anaconda distribution). If the details are relevant to this issue, please go to a more specific question .

+13
python scikit-learn multiprocessing runtime-error anaconda
source share
1 answer

On Windows, all of your multiprocessing -using code must be protected if __name__ == "__main__":

So, to be safe, I would put all your code at the top level of your script in the main() function, and then just do it at the top level:

 if __name__ == "__main__": main() 

See the “Secure Import of the Main Module” section here for an explanation of why this is necessary. You probably do not need to call freeze_support at freeze_support , although that doesn’t prevent it from being turned on.

Please note that in any case, it is recommended that you use if __name__ == "__main__" guard for scripts, so the code will not be executed unexpectedly if you find that you need to import your script into another script at some point in the future.

+33
source share

All Articles