我正在windows机器上try 使用线程和多处理的第一个正式python程序.不过,我无法启动这些进程,python给出了以下消息.问题是,我没有在main模块中启动我的线程.线程在类中的一个单独模块中处理.

EDIT:顺便说一下,这段代码在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()

推荐答案

在Windows上,子进程将在启动时导入(即执行)主模块.您需要在主模块中插入一个if __name__ == '__main__':保护,以避免递归地创建子流程.

修改后的testMain.py:

import parallelTestModule

if __name__ == '__main__':    
    extractor = parallelTestModule.ParallelExtractor()
    extractor.runInParallel(numProcesses=2, numThreads=4)

Python相关问答推荐

如何在__init__.py中调整我的外部自定义库导入?

从pandas中的两列创建分类数据

学习率未更新

如何随着收件箱的增加动态添加到HTML表的右下角?

在IIS中运行的FastAPI-获取权限错误:[Win错误10013]试图以其访问权限禁止的方式访问插槽

如何循环循环的每个元素并过滤掉Python rame中的条件

将大小为n*512的数组绘制到另一个大小为n*256的数组的PC组件

在Python中根据id填写年份系列

使用Python进行网页抓取,没有页面

使用Python Great Expectations和python-oracledb

如何在Python中使用ijson解析SON期间检索文件位置?

具有症状的分段函数:如何仅针对某些输入值定义函数?

根据网格和相机参数渲染深度

Pytest两个具有无限循环和await命令的Deliverc函数

发生异常:TclMessage命令名称无效.!listbox"

Mistral模型为不同的输入文本生成相同的嵌入

计算分布的标准差

Python导入某些库时非法指令(核心转储)(beautifulsoup4."" yfinance)

索引到 torch 张量,沿轴具有可变长度索引

python中的解释会在后台调用函数吗?