python - 在无限循环中并行运行多个脚本

标签 python bash shell unix

我有 20 个 python 脚本,我想在不同的 bash 窗口中并行运行它们,我可以使用以下命令并在后端并行运行它们:-

python testv.py &
python testv1.py &
python testv2.py &
python testv3.py &
python testv4.py &
python testv5.py &
python testv6.py &
python testv7.py &
python testv8.py &
python testv9.py &
python testv10.py &
python testv11.py &
python testv12.py &
python testv13.py &
python testv14.py &
python testv15.py &
python testv16.py &
python testv17.py &
python testv18.py &
python testv19.py &
python testv20.py &

我在 bash 脚本中转换了上面的内容:- vaa.sh

#!/bin/bash
python testv.py &
python testv1.py &
python testv2.py &
python testv3.py &
python testv4.py &
python testv5.py &
python testv6.py &
python testv7.py &
python testv8.py &
python testv9.py &
python testv10.py &
python testv11.py &
python testv12.py &
python testv15.py &
python testv16.py &
python testv17.py &
python testv18.py &
python testv19.py &
python testv20.py &

我想运行这个 2-3 小时或者说永远运行直到干预。我怎样才能实现这个目标。

我尝试在 cronjob 中添加 vaa.sh 15 分钟,但我想以某种方式执行此操作,以便一旦脚本完成,它就应该再次开始,无论总时间是 15 分钟或 20 分钟。

最佳答案

您可以使用 multiprocessing 来做到这一点

import os
import time
from multiprocessing import Process

def run_program(cmd):
    # Function that processes will run
    os.system(cmd)

# Creating command to run
commands = ['python testv.py']
commands.extend(['python testv{}.py'.format(i) for i in range(1, 21)])

# Amount of times your programs will run
runs = 1

for run in range(runs):
    # Initiating Processes with desired arguments
    running_programs = []
    for command in commands:
        running_programs.append(Process(target=run_program, args=(command,)))
        running_programs[-1].daemon = True

    # Start our processes simultaneously
    for program in running_programs:
        program.start()

    # Wait untill all programs are done
    while any(program.is_alive() for program in running_programs):
        time.sleep(1)
<小时/>

如果您想在一段时间后停止执行程序,您可以这样做。

desired_time = 2 * 60 * 60 # 2 Hours into seconds
start_time = time.time()

while True:
    # Initiating Processes with desired arguments
    running_programs = []
    for command in commands:
        running_programs.append(Process(target=run_program, args=(command,)))
        running_programs[-1].daemon = True

    # Start our processes simultaneously
    for program in running_programs:
        program.start()

    # Wait untill all programs are done or time has passed
    while any(program.is_alive() for program in running_programs) and time.time() - start_time < desired_time:
        time.sleep(1)

    # If desired time has passed exit main loop
    if time.time() - start_time > desired_time:
        break

关于python - 在无限循环中并行运行多个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53526584/

相关文章:

python - 解构 if/else Python

python - 从两个字典创建一个新列表(不区分大小写)

python - 如何制作圆形按钮 tkinter?

bash - 别名参数不起作用

shell - 为什么 emacs comint 模式不将字符串作为 shell 处理?

python - 如何在 colab.research 上安装 rpy2?

bash - 如何在 BASH 中包含匹配模式的任何行之前添加#?

bash - 如何使用shell计算String中的单词数

C Linux shell - 重复最近的命令

python - 使用 python 运行 windows shell 命令