swansonk14 / p_tqdm

Parallel processing with progress bars
MIT License
457 stars 44 forks source link

tqdm.write #23

Open joeyorlando opened 4 years ago

joeyorlando commented 4 years ago

Great library! I'm not sure whether this is something you've already looked into/tried or if this would be a new feature addition.

Anyhow, I have a script I am running using p_tqdm and I'd like to achieve something similar to tqdm.write where you can have the progress bar fixed to the bottom whilst printed messages to stdout end up above that. The script is rather large and rather than manually going in and changing those print statements I've borrowed a SO answer to overload print.

A simple repro script is as follows:

import time
import inspect
from p_tqdm import p_map
from p_tqdm.p_tqdm import tqdm  #  NOTE: here I've also tried importing tqdm as `from tqdm.auto import tqdm`.. no luck

def divert_stdout_to_tqdm() -> None:
    old_print = print

    def new_print(*args, **kwargs) -> None:
        # if tqdm.tqdm.write raises error, use builtin print
        try:
            tqdm.write(*args, **kwargs)
        except:
            old_print(*args, ** kwargs)

    inspect.builtins.print = new_print

def do_stuff(num: int) -> None:
    print("HIIIIII")
    time.sleep(0.5)

divert_stdout_to_tqdm()

# doesn't work
results = p_map(do_stuff, range(100))

# works
for i in tqdm(range(100)):
    do_stuff(i)
results = p_map(do_stuff, range(100))

doesn't work exactly as I'd intend as it produces output as such:

HIIIIII
  0%|                                                                                                   | 0/100 [00:00<?, ?it/s]HIIIIII
HIIIIII
  1%|▉                                                                                          | 1/100 [00:00<01:35,  1.03it/s]HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
 17%|███████████████▎                                                                          | 17/100 [00:01<00:57,  1.44it/s]HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
HIIIIII
for i in tqdm(range(100)):
    do_stuff(i)

works as intended and produces output as such:

HIIIIII                                                                                                                         
HIIIIII                                                                                                                         
HIIIIII                                                                                                                         
HIIIIII                                                                                                                         
HIIIIII                                                                                                                         
HIIIIII                                                                                                                         
  5%|████▌                                                                                      | 5/100 [00:02<00:47,  1.99it/s]

I am also not an expert in the multiprocessing library and it is very well possible that this is more related to multiprocessing than it is p_tqdm.

Irazall commented 4 years ago

What is your OS and your IDE? This might be important. In Windows there are issues with tqdm for example.

joeyorlando commented 4 years ago

macOS (Catalina v10.15.4) and Visual Studio Code