简体   繁体   English

从 Python 调用 multiple.exe

[英]Call multiple .exe from Python

I have a problem where I want to call multiple.exe files from Python at once and one.exe has like 5 arguments.我有一个问题,我想一次从 Python 调用 multiple.exe 文件,而 one.exe 有 5 个 arguments。 How can I call it?我怎么称呼它? I tried os.system("process.exe arg1 arg2 arg3 arg4") which works but it can only launch one process at time and I also tried subprocess.run, however while it can call process like subprocess.run("process.exe") I can't seem to find solution how to send arguments like subprocess.run("process.exe arg1 arg2 arg3") .我尝试os.system("process.exe arg1 arg2 arg3 arg4") ,但它一次只能启动一个进程,我也尝试了 subprocess.run,但是它可以调用像subprocess.run("process.exe")我似乎无法找到如何发送 arguments 之类的解决方案 subprocess.run subprocess.run("process.exe arg1 arg2 arg3") I need it to also open commandline as stdout which os.system command can do.我还需要它来打开命令行作为 os.system 命令可以执行的标准输出。

If you want to spawn multiple processes simultaneously ("asynchronously", "in parallel"), use如果要同时生成多个进程(“异步”、“并行”),请使用

p = subprocess.Popen("<command> <args>", shell=True, stdout=subprocess.PIPE)

which instantly returns the Popen instance (object representing the spawned process).它立即返回 Popen 实例(代表生成进程的对象)。

shell=True allows you to pass the command as a "simple string" (eg "myprog arg1 arg2" ). shell=True允许您将命令作为“简单字符串”(例如"myprog arg1 arg2" )传递。 Without shell=True you must pass the list of arguments, including the name of a program as a first arg (eg ["myprog", "arg1", "arg2"] ).如果没有shell=True ,您必须传递 arguments 列表,包括作为第一个 arg 的程序名称(例如["myprog", "arg1", "arg2"] )。

stdout=PIPE ensures stdout capturing in a pipe (a file-like object in Python terms), which may be accessed via p.stdout . stdout=PIPE确保在 pipe(Python 术语中的 object 中的类似文件)中捕获标准输出,可以通过p.stdout访问。 Use can read the output of your program via p.stdout.read().decode() , but note that this call is blocking , ie it awaits the program to stop, so in your case make sure that you have spawned all necessary processes before reading their outputs.使用可以通过p.stdout.read().decode()读取程序的 output ,但请注意此调用是阻塞的,即它等待程序停止,因此在您的情况下,请确保您已生成所有必要的进程在阅读他们的输出之前。

In order to await the program without reading its output, use p.wait() .为了等待程序而不读取其 output,请使用p.wait()

More information in Python docs: subprocess . Python 文档中的更多信息: subprocess

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM