Python 多处理使用 argparse 和 pyinstaller 引发错误 [英] Python multiprocessing throws error with argparse and pyinstaller

查看:36
本文介绍了Python 多处理使用 argparse 和 pyinstaller 引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用 argprse 来传递参数,并且在脚本的某个地方我使用多处理来完成其余的计算.如果我从命令提示符调用它,脚本工作正常例如.

In my project, I'm using argprse to pass arguments and somewhere in script I'm using multiprocessing to do rest of the calculations. Script is working fine if I call it from command prompt for ex.

"python complete_script.py --arg1=xy --arg2=yz" .

但是在使用 Pyinstaller 使用命令pyinstaller --onefile complete_script.py"将其转换为 exe 后,它抛出

But after converting it to exe using Pyinstaller using command "pyinstaller --onefile complete_script.py" it throws

错误

" 错误:无法识别的参数:--multiprocessing-fork 1448"

" error: unrecognized arguments: --multiprocessing-fork 1448"

有什么建议可以让我完成这项工作.或任何其他选择.我的目标是创建一个 exe 应用程序,我可以在未安装 Python 的其他系统中调用该应用程序.

Any suggestions how could I make this work. Or any other alternative. My goal is to create an exe application which I can call in other system where Python is not installed.

这是我的工作站的详细信息:

Here are the details of my workstation:

Platform: Windows 10
Python : 2.7.13 <installed using Anaconda>
multiprocessing : 0.70a1
argparse:   1.1

<小时>

从评论中复制:


Copied from comment:

def main():
     main_parser = argparse.ArgumentParser()
     < added up arguments here>
    all_inputs = main_parser.parse_args()
    wrap_function(all_inputs)


def wrap_function(all_inputs):
    <Some calculation here >
   distribute_function(<input array for multiprocessing>)

def distribute_function(<input array>):
    pool = Pool(process = cpu_count)
    jobs = [pool.apply_async(target_functions, args = (i,) for i in input_array)]
    pool.close()

推荐答案

(有点晚了,但是以后对别人有用...)

(A bit late but it can be useful for someone else in the future...)

我遇到了同样的问题,经过一番研究,我发现了这个多处理 pyInstaller 配方 声明:

I had the same problem, after some research I found this multiprocessing pyInstaller recipe that states:

使用多处理模块时,必须调用

When using the multiprocessing module, you must call

multiprocessing.freeze_support()

紧接在主模块的 if __name__ == '__main__': 行之后.

straight after the if __name__ == '__main__': line of the main module.

请阅读关于 multiprocessing.freeze_support 的Python 库手册了解更多信息.

Please read the Python library manual about multiprocessing.freeze_support for more information.

添加那行代码为我解决了问题.

Adding that line of code solved the problem for me.

这篇关于Python 多处理使用 argparse 和 pyinstaller 引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆