python中的进程

为什么multiprocessing的Process类必须要在if __name__="__main__"下调用,否则报错?这种情况只在Windows中存在,无法理解网上的解释

Since Windows has no fork, the multiprocessing module starts a new Python process and imports the calling module. If Process() gets called upon import, then this sets off an infinite succession of new processes (or until your machine runs out of resources). This is the reason for hiding calls to Process() insideif name == "__main__"since statements inside this if-statement will not get called upon import.
链接:https://www.zhihu.com/questio...
阅读 1.4k
1 个回答

這段網文只道出因為windows沒有fork, 然後又說windows python 用multiprocessing module 實現多進程。。。,實現了process() 的代碼如果被import而沒有ifXxxxxxxx的話會有死循環。
其實fork 是unix和linix 實現多進程的方式,跟windows有分別,樓主先去理解fork 吧, 抱歉,我不懂,只是提出個方向

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题