尝试 python 多处理的 Windows 上的 RuntimeError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18204782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RuntimeError on windows trying python multiprocessing
提问by NG Algo
I am trying my very first formal python program using Threading and Multiprocessing on a windows machine. I am unable to launch the processes though, with python giving the following message. The thing is, I am not launching my threads in the mainmodule. The threads are handled in a separate module inside a class.
我正在 Windows 机器上使用线程和多处理尝试我的第一个正式的 python 程序。我无法启动进程,python 给出以下消息。问题是,我没有在主模块中启动我的线程。线程在类内的单独模块中处理。
EDIT: By the way this code runs fine on ubuntu. Not quite on windows
编辑:顺便说一下,这段代码在 ubuntu 上运行良好。不太在窗户上
RuntimeError:
Attempt to start a new process before the current process
has finished its bootstrapping phase.
This probably means that you are on Windows and you have
forgotten to use the proper idiom in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce a Windows executable.
My original code is pretty long, but I was able to reproduce the error in an abridged version of the code. It is split in two files, the first is the main module and does very little other than import the module which handles processes/threads and calls a method. The second module is where the meat of the code is.
我的原始代码很长,但我能够在代码的删节版中重现该错误。它分为两个文件,第一个是主模块,除了导入处理进程/线程和调用方法的模块外,几乎没有其他作用。第二个模块是代码的核心所在。
testMain.py:
测试Main.py:
import parallelTestModule
extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
parallelTestModule.py:
并行测试模块.py:
import multiprocessing
from multiprocessing import Process
import threading
class ThreadRunner(threading.Thread):
""" This class represents a single instance of a running thread"""
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print self.name,'\n'
class ProcessRunner:
""" This class represents a single instance of a running process """
def runp(self, pid, numThreads):
mythreads = []
for tid in range(numThreads):
name = "Proc-"+str(pid)+"-Thread-"+str(tid)
th = ThreadRunner(name)
mythreads.append(th)
for i in mythreads:
i.start()
for i in mythreads:
i.join()
class ParallelExtractor:
def runInParallel(self, numProcesses, numThreads):
myprocs = []
prunner = ProcessRunner()
for pid in range(numProcesses):
pr = Process(target=prunner.runp, args=(pid, numThreads))
myprocs.append(pr)
# if __name__ == 'parallelTestModule': #This didnt work
# if __name__ == '__main__': #This obviously doesnt work
# multiprocessing.freeze_support() #added after seeing error to no avail
for i in myprocs:
i.start()
for i in myprocs:
i.join()
采纳答案by Janne Karila
On Windows the subprocesses will import (i.e. execute) the main module at start. You need to insert an if __name__ == '__main__':
guard in the main module to avoid creating subprocesses recursively.
在 Windows 上,子进程将在启动时导入(即执行)主模块。您需要if __name__ == '__main__':
在主模块中插入保护以避免递归创建子进程。
Modified testMain.py
:
修改testMain.py
:
import parallelTestModule
if __name__ == '__main__':
extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
回答by doctorlove
Try putting your code inside a main function in testMain.py
尝试将您的代码放在 testMain.py 中的 main 函数中
import parallelTestModule
if __name__ == '__main__':
extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
See the docs:
查看文档:
"For an explanation of why (on Windows) the if __name__ == '__main__'
part is necessary, see Programming guidelines."
which say
其中说
"Make sure that the main module can be safely imported by a new Python interpreter without causing unintended side effects (such a starting a new process)."
“确保新的 Python 解释器可以安全地导入主模块,而不会导致意外的副作用(例如启动新进程)。”
... by using if __name__ == '__main__'
... 通过使用 if __name__ == '__main__'
回答by Ofer
Though the earlier answers are correct, there's a small complication it would help to remark on.
虽然较早的答案是正确的,但有一个小的复杂性有助于评论。
In case your main module imports another module in which global variables or class member variables are defined and initialized to (or using) some new objects, you may have to condition that import in the same way:
如果您的主模块导入另一个模块,其中定义了全局变量或类成员变量并将其初始化为(或使用)一些新对象,您可能必须以相同的方式调整导入:
if __name__ == '__main__':
import my_module
回答by Luis Abdi
As @Ofer said, when you are using another libraries or modules, you should import all of them inside the if __name__ == '__main__':
正如@Ofer 所说,当您使用其他库或模块时,您应该将它们全部导入到 if __name__ == '__main__':
So, in my case, ended like this:
所以,就我而言,以这样的方式结束:
if __name__ == '__main__':
import librosa
import os
import pandas as pd
run_my_program()