Why is it important to protect the main loop when using joblib.Parallel?

后端 未结 1 437
北海茫月
北海茫月 2020-12-18 18:40

The joblib docs contain the following warning:

Under Windows, it is important to protect the main loop of code to avoid recursive spawning of subpro

相关标签:
1条回答
  • 2020-12-18 19:16

    This is necessary because Windows doesn't have fork(). Because of this limitation, Windows needs to re-import your __main__ module in all the child processes it spawns, in order to re-create the parent's state in the child. This means that if you have the code that spawns the new process at the module-level, it's going to be recursively executed in all the child processes. The if __name__ == "__main__" guard is used to prevent code at the module scope from being re-executed in the child processes.

    This isn't necessary on Linux because it does have fork(), which allows it to fork a child process that maintains the same state of the parent, without re-importing the __main__ module.

    0 讨论(0)
提交回复
热议问题