Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tqdm.write #23

Open
joeyorlando opened this issue May 25, 2020 · 2 comments
Open

tqdm.write #23

joeyorlando opened this issue May 25, 2020 · 2 comments

Comments

@joeyorlando
Copy link

joeyorlando commented May 25, 2020

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
Copy link

Irazall commented Jun 10, 2020

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

@joeyorlando
Copy link
Author

macOS (Catalina v10.15.4) and Visual Studio Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants