无限循环并行运行多个脚本

时间:2018-11-28 19:16:57

标签: 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分钟。

2 个答案:

答案 0 :(得分:0)

JSON

答案 1 :(得分:0)

您可以使用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