我正在尝试在Windows机器上使用线程和多处理的第一个正式的python程序。我无法启动进程,python提供以下消息。问题是,我没有在主模块中启动我的线程。线程在类中的单独模块中处理。
编辑:顺便说一句,这段代码在ubuntu上正常运行。不完全在Windows上
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)
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()
答案 0 :(得分:93)
在Windows上,子进程将在启动时导入(即执行)主模块。您需要在主模块中插入if __name__ == '__main__':
保护,以避免递归创建子进程。
修改后的testMain.py
:
import parallelTestModule
if __name__ == '__main__':
extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
答案 1 :(得分:18)
尝试将代码放在testMain.py
中的main函数中import parallelTestModule
if __name__ == '__main__':
extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
请参阅docs:
"For an explanation of why (on Windows) the if __name__ == '__main__'
part is necessary, see Programming guidelines."
说
“确保新模块可以安全地导入主模块 口译员没有引起意外的副作用(这样的开始a 新进程)。“
...使用if __name__ == '__main__'
答案 2 :(得分:4)
虽然早期的答案是正确的,但是有一点很小的复杂性会引起注意。
如果您的主模块导入另一个模块,其中定义全局变量或类成员变量并将其初始化为(或使用)某些新对象,您可能必须以相同的方式调整导入:
if __name__ == '__main__':
import my_module
答案 3 :(得分:2)
您好,这是我的多进程结构
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)')
您不必将导入内容放在名称 =='主要”中,只需运行要在其中运行的程序即可
答案 4 :(得分:0)
如@Ofer所说,当您使用其他库或模块时,应将它们全部导入if __name__ == '__main__':
因此,就我而言,这样结束:
if __name__ == '__main__':
import librosa
import os
import pandas as pd
run_my_program()
答案 5 :(得分:0)
就我而言,这是代码中的一个简单错误,在创建变量之前就使用了变量。在尝试上述解决方案之前,值得检查一下。上帝知道,为什么我会收到此特定错误消息。