从一个程序运行多个程序 [英] Running several programs from one program

查看:131
本文介绍了从一个程序运行多个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有12个程序,我打算同时运行。有没有办法通过一个程序运行所有这些程序,当构建运行12个程序时?

I have 12 programs which i intend to run simultaneously. Is there any way i run all of them via one single program which when built runs the 12 programs?

我使用的是sublime,程序是python。

I am using sublime and the programs are python.

推荐答案

如果您只想批量执行程序,可以在bash脚本中执行。假设它们是同一文件夹中的可执行文件,您可以使用以下内容获得 .sh 文件:

If you just want to execute the programs one by one in a batch, you can do it in a bash script. Assuming they are executables in the same folder, you can have an .sh file with the following contents:

#!/bin/bash
python ./my_app1.py &
python ./my_app2.py &
python ./my_app3.py

如果脚本本身具有#!/ usr / bin / env python 在顶部标识解释器,你可以在它们上面执行 chmod + x ,然后转动你的 runner.sh file into:

If the scripts themselves have the #!/usr/bin/env python at the top to identify the interpreter, you can do chmod +x on them, and turn your runner.sh file into:

#!/bin/bash
./my_app1.py &
./my_app2.py &
./my_app3.py

另一方面,如果你想从一个python脚本,你可以使用:

On the other hand, if you want to do this from a python script, you can use:

import subprocess
import os

scripts_to_run = ['my_app1.py', 'my_app2.py', 'my_app3.py']

for s in scripts_to_run:
    subprocess.Popen([os.path.join(os.getcwd(), s)])

注1 :不要忘记在第一行的每个脚本中包含#!/ usr / bin / env python

注意2 :重要的是使用 subprocess.Popen()而不是 subprocess.call()因为后者是一个阻塞函数,在继续之前会等待申请完成。使用 subproces.Popen(),您将获得并发执行。

Note 1: don't forget to include the #!/usr/bin/env python in every script on the first line.
Note 2: it is important to use subprocess.Popen() instead subprocess.call() because the latter is a blocking function that will wait for the application to finish before proceeding. With subproces.Popen() you get the concurrent execution.

这篇关于从一个程序运行多个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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