尝试python多处理的Windows上的RuntimeError

尝试python多处理的Windows上的RuntimeError,第1张

我正在尝试在 Windows 机器上使用线程和多处理的第一个正式的 python 程序。我无法启动这些进程,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.

我的原始代码很长,但我能够在代码的删节版本中重现该错误。它分为两个文件,第一个是主模块,除了导入处理进程/线程和调用方法的模块外,它几乎没有做任何事情。第二个模块是代码的核心所在。


测试主.py:

import parallelTestModuleextractor = parallelTestModule.ParallelExtractor()extractor.runInParallel(numProcesses=2, numThreads=4)

并行测试模块.py:

import multiprocessingfrom multiprocessing import Processimport threadingclass 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()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/read/1369986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-15
下一篇 2022-06-15

发表评论

登录后才能评论
保存