windows尝试python多处理时出现运行时错误

windows尝试python多处理时出现运行时错误,python,windows,multiprocessing,Python,Windows,Multiprocessing,我正在windows机器上尝试使用线程和多处理的第一个正式python程序。但是我无法启动进程,python给出了以下消息。问题是,我没有在主模块中启动线程。线程在类内的单独模块中处理 编辑:顺便说一下,这段代码在ubuntu上运行良好。在窗户上不太合适 RuntimeError: Attempt to start a new process before the current process has finished its bootstra

我正在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.
我的原始代码相当长,但我能够在代码的简化版本中重现错误。它被分为两个文件,第一个是主模块,除了导入处理进程/线程和调用方法的模块外,几乎没有其他功能。第二个模块是代码的核心所在


testMain.py:

import parallelTestModule

extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
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()

parallelTestModule.py:

import parallelTestModule

extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
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上,子进程将在启动时导入(即执行)主模块。您需要在主模块中插入
如果uuuu name_uuuu=='\uuuuu main\uuuuu':
保护,以避免递归创建子流程

修改的
testMain.py

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)
见:

上面说

“确保新Python可以安全地导入主模块 在不引起意外副作用的情况下(如启动 新工艺。”


。。。通过使用
如果uuuu name uuu='\uuuu main uuu'
尽管前面的答案是正确的,但有一个小问题需要注意

如果主模块导入另一个模块,其中定义了全局变量或类成员变量,并将其初始化为(或使用)一些新对象,则您可能必须以相同的方式对导入进行调整:

if __name__ ==  '__main__':
  import my_module

正如@Ofer所说,当您正在使用另一个库或模块时,您应该在
中导入所有这些库或模块,如果

在我的例子中,结果是这样的:

if __name__ == '__main__':       
    import librosa
    import os
    import pandas as pd
    run_my_program()

在我的例子中,这是代码中的一个简单错误,在创建变量之前使用了一个变量。在尝试上述解决方案之前,值得检查一下。天知道我为什么会收到这个错误消息。

你好,这是我的多进程结构

from multiprocessing import Process
import time


start = time.perf_counter()


def do_something(time_for_sleep):
    print(f'Sleeping {time_for_sleep} second...')
    time.sleep(time_for_sleep)
    print('Done Sleeping...')



p1 = Process(target=do_something, args=[1])
p2 = Process(target=do_something, args=[2])


if __name__ == '__main__':
    p1.start()
    p2.start()

    p1.join()
    p2.join()

    finish = time.perf_counter()
    print(f'Finished in {round(finish-start,2 )} second(s)')

如果,则不必将导入放入
,只需运行希望在内部运行的程序

@doctorlove我以python testMain.pySure的形式运行它-您需要一个if name==“main”查看答案和docs@NGAlgo在我调试pymongo和多处理问题时,您的脚本对我非常有帮助。谢谢(手掌拍着额头)啊!它有效!!!!非常感谢你!我忽略了一个事实,那就是重新导入的是原始的主模块!在启动进程之前,我一直在尝试“name==”检查。我似乎无法导入“parallelTestModule”。我正在使用Python 2.7。它应该开箱即用吗?@Jonny parallelTestModule.py的代码是问题的一部分。@DeshDeepSingh代码段不是一个独立的例子;这是对OP的修改code@DeshDeepSingh该模块是问题的一部分。