[Python] Printing status report on asynchronous task in python

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I have a python script that uses multiprocessing to run a function with given args and prints when it starts, when it finishes and how long it took. Assuming a simple case, the code would be like something like the following:

    import concurrent.futures
    import datetime

    def do_stuff(arg):
    start_time = datetime.now()
    # do stuff
    end_time = datetime.now()

    return f"Finished task with arg {arg} in {end_time-start_time}"

    if __name__ == '__main__':
    with concurrent.futures.ProcessPoolExecutor() as executor:
    results = []

    for arg in args: # args is a list of arguments to be passed
    print(f"Starting task with arg {arg}")
    results.append(executor.submit(do_stuff, arg))

    for f in concurrent.futures.as_completed(results):
    print(f.result())


    Let's assume that the args to be passed are arg1, arg2, arg3 and arg4 and the start and end of tasks happens in the following order:

    1. Task with arg1 starts
    2. Task with arg2 starts
    3. Task with arg3 starts
    4. Task with arg2 finishes
    5. Task with arg4 starts
    6. Task with arg1 finishes
    7. Task with arg4 finishes
    8. Task with arg3 finishes

    The terminal should end something like the following:

    Starting task with arg arg1
    Starting task with arg arg2
    Starting task with arg arg3
    Finished task with arg arg2 in mm:ss
    Starting task with arg arg4
    Finished task with arg arg1 in mm:ss
    Finished task with arg arg4 in mm:ss
    Finished task with arg arg3 in mm:ss


    I want a way of reporting the start and end of a task on the same line such that the terminal look as follows in each step.


    1. Task with arg1 starts

      Starting task with arg arg1

    2. Task with arg2 starts

      Starting task with arg arg1
      Starting task with arg arg2

    3. Task with arg3 starts

      Starting task with arg arg1
      Starting task with arg arg2
      Starting task with arg arg3

    4. Task with arg2 finishes

      Starting task with arg arg1
      Starting task with arg arg2 - finished in mm:ss
      Starting task with arg arg3

    5. Task with arg4 starts

      Starting task with arg arg1
      Starting task with arg arg2 - finished in mm:ss
      Starting task with arg arg3
      Starting task with arg arg4

    6. Task with arg1 finishes

      Starting task with arg arg1 - finished in mm:ss
      Starting task with arg arg2 - finished in mm:ss
      Starting task with arg arg3
      Starting task with arg arg4

    7. Task with arg4 finishes

      Starting task with arg arg1 - finished in mm:ss
      Starting task with arg arg2 - finished in mm:ss
      Starting task with arg arg3
      Starting task with arg arg4 - finished in mm:ss

    8. Task with arg3 finishes

      Starting task with arg arg1 - finished in mm:ss
      Starting task with arg arg2 - finished in mm:ss
      Starting task with arg arg3 - finished in mm:ss
      Starting task with arg arg4 - finished in mm:ss

    I know that there are ANSI escape codes that let me move the cursor up and down, but I would have to store somewhere the line position of each task so that when it finishes, the cursor can move back up to this line and rewrite the text. I would also have to have some way of tracking the "current" line to know how much to go back up and how much to go back down. But this sounds like a pain to do and I'm here wondering with something already exists for this type of thing. Any help is appreciated.

    Continue reading...

Compartilhe esta Página