尝试python多处理的Windows上的RuntimeError
我正在尝试我的第一个正式的Python程序在Windows机器上使用线程和多处理。 我无法启动这个过程,用python给出下面的消息。 事情是,我不在主模块中启动我的线程。 线程在一个类中的独立模块中处理。
编辑 :顺便说一句,这个代码在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.
我的原始代码很长,但是我能够在代码的删节版本中重现错误。 它分为两个文件,第一个是主模块,除了导入处理进程/线程的模块和调用方法以外,其他function都很less。 第二个模块是代码的肉。
testMain.py:
import parallelTestModule extractor = parallelTestModule.ParallelExtractor() extractor.runInParallel(numProcesses=2, numThreads=4)
parallelTestModule.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()
在Windows上,subprocess将在启动时导入(即执行)主模块。 您需要像这样保护主代码,以避免以recursion方式创buildsubprocess:
import parallelTestModule if __name__ == '__main__': extractor = parallelTestModule.ParallelExtractor() extractor.runInParallel(numProcesses=2, numThreads=4)
尝试把你的代码放在testMain.py的主函数中
import parallelTestModule if __name__ == '__main__': extractor = parallelTestModule.ParallelExtractor() extractor.runInParallel(numProcesses=2, numThreads=4)
看文档 :
"For an explanation of why (on Windows) the if __name__ == '__main__' part is necessary, see Programming guidelines."
这就是说
“确保主模块可以被一个新的Python解释器安全地导入,而不会造成意想不到的副作用(例如开始一个新的过程)”。
…通过使用if __name__ == '__main__'
虽然早先的答案是正确的,但是有一点小小的复杂性可以帮助您评论。
如果你的主模块导入另一个模块,其中全局variables或类成员variables被定义并初始化为(或使用)一些新的对象,则可能必须以相同的方式调整该导入:
如果name ==' main '':import my_module