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

查看:806
本文介绍了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天全站免登陆