rich icon indicating copy to clipboard operation
rich copied to clipboard

[REQUEST] Get elapsed time from Task in Progress

Open staehle opened this issue 3 years ago • 0 comments

Hello! Rich has been super useful, to the point that I'm replacing a very similar home-built multi-progress-bar display solution with it. Now, perhaps this is something that already exists, but it wasn't obvious how to do it.

When using Progress, how do you get the final Task's elapsed time, in order to print that out to a log file?

I'm currently doing this, which while it works, doesn't seem right:

with rich.progress.Progress(*STD_PROGRESS_COLS, expand=True) as progress:
    tasks = []
    for i, thing in enumerate(things_to_do):
        tasks.append(progress.add_task(f"[blue]Thing {i}:[/] {thing}"))

        <threaded_stuff>(thing, tasks[i], progress)

    <threaded_stuff>.join()

    with path_log.open(mode='a') as lfp:
        lfp.write("Final times per task:\n")
        for i, thing in enumerate(things_to_do):
            # pylint:disable=protected-access                         # <-- Feels dirty
            f_time = progress._tasks[tasks[i]].elapsed                # <-- Probably shouldn't do this
            s_time = time.strftime("%Hh:%Mm:%Ss", time.gmtime(f_time))
            lfp.write(f" Task {i}, {thing}: {s_time}")

Is there a better way? I would just add an extra timer, but it seems like I should be able to just get the existing timer values from Task.

If directly accessing the private _tasks list is currently the only option, Here's a PR that adds a getter for that value from the Progress object, if this is a better way of doing it: #2345

staehle avatar Jun 15 '22 16:06 staehle