打包具有多处理代码的python模块

问题描述 投票:0回答:1

我正在尝试使用pyinstaller将python项目打包为可执行文件。主模块包含用于多处理的代码。当我运行可执行文件时,只有多处理部分之前的代码行一次又一次地执行。它既不会引发异常也不退出程序。

主模块中的代码:

from Framework.ExcelUtility import ExcelUtility
from Framework.TestRunner import TestRunner
import concurrent.futures


class Initiator:

def __init__(self):
    self.exec_config_dict = {}
    self.test_list = []
    self.test_names = []
    self.current_test_set = []

def set_first_execution_order(self):
    # Code


def set_subsequent_execution_order(self):
    # Code

def kick_off_tests(self):
    '''Method to do Multi process execution'''
    if(__name__=="__main__"):
        with concurrent.futures.ProcessPoolExecutor(max_workers=int(self.exec_config_dict.get('Parallel'))) as executor:
            for test in self.current_test_set:
                executor.submit(TestRunner().runner,test)  ***This line is not being executed from the exe file.


initiator = Initiator()
initiator.get_run_info()
initiator.set_first_execution_order()
initiator.kick_off_tests()
while len(initiator.test_list) > 0:
    initiator.set_subsequent_execution_order()
    try:
        initiator.kick_off_tests()
    except BaseException as exception:
        print(exception)
python pyinstaller python-multiprocessing python-packaging
1个回答
0
投票

从问题定义中,我假设您正在使用ms-windows,并且主模块未命名为__main__.py

在这种情况下,multiprocessing有一些特殊准则:

确保主模块可以被新的Python解释器安全地导入,而不会引起意外的副作用(例如,启动新进程)。>>

相反,应该使用if __name__ == '__main__'保护程序的“入口点”>

因此,像这样更改主模块的最后一部分:

from multiprocessing import freeze_support

def kick_off_tests(self):
    '''Method to do Multi process execution'''
        with concurrent.futures.ProcessPoolExecutor(max_workers=int(self.exec_config_dict.get('Parallel'))) as executor:
            for test in self.current_test_set:
                executor.submit(TestRunner().runner,test)  


if __name__ == '__main__':
    freeze_support()
    initiator = Initiator()
    initiator.get_run_info()
    initiator.set_first_execution_order()
    initiator.kick_off_tests()
    while len(initiator.test_list) > 0:
        initiator.set_subsequent_execution_order()
        try:
            initiator.kick_off_tests()
        except BaseException as exception:
            print(exception)
© www.soinside.com 2019 - 2024. All rights reserved.