[Python] Spread out task evenly over a minute, some tasks will run multiple times and should...

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a conifg file with a list of bash script that should run times_per_min amount of time(s) during 60 seconds.

    tasks.yaml:

    every_minute:
    - name: test0
    path: test0.sh
    times_per_min: 3
    - name: test1
    path: test1.sh
    times_per_min: 2


    My python code to import the tasks and run them:

    import subprocess
    import time
    import yaml

    TASKS = []

    with open("tasks.yaml") as file:
    measurements = yaml.safe_load(file)

    for every_min in measurements["every_minute"]:
    for _ in range(every_min["times_per_min"]):
    TASKS.append(every_min["path"])

    interval = 60 / len(TASKS)

    for task in TASKS:
    try:
    PROCESSES.append(subprocess.Popen([task]))
    except Exception as e:
    print("An error occured")
    time.sleep(interval)


    In this case, TASKS would look like:

    print(TASKS)
    ['test0.sh', 'test0.sh', 'test0.sh', 'test1.sh', 'test1.sh']


    What I would like: ['test0.sh', 'test1.sh', 'test0.sh', 'test1.sh', 'test0.sh']

    Also note that I want this distribution to work no matter the amount of tasks and the times_per_min value...

    Continue reading...

Compartilhe esta Página