简体   繁体   English

在 Python 中同时运行可执行文件

[英]Running executables concurrently in Python

I have 3 folders, namely, 1 , 2 , 3 each with the following executable.我有 3 个文件夹,即123每个文件夹都有以下可执行文件。 How can I run all of them concurrently at once?我怎样才能同时运行所有这些?

exec(open("Test.py").read())

Since you want to run these.py files as separate processes, you should be able to execute them via the same python executable your main script is using.由于您希望将这些.py 文件作为单独的进程运行,因此您应该能够通过您的主脚本正在使用的相同 python 可执行文件来执行它们。 Use Popen to start all of the processes and then wait on them in turn.使用Popen启动所有进程,然后依次等待它们。

#!/usr/bin/env python3

import os
import sys
import subprocess as subp

folder_names = ["1", "2", "3"]
procs = [subp.Popen([sys.executable, os.path.join(folder, "Test.py"])
    for folder in folder_names]
for proc in procs:
    return_code = proc.wait()

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

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