1

I'm trying to show multithreading process with progress bars so I used "tqdm" and this is my code:

from time import sleep
from tqdm import tqdm
import threading

def one():
    for i in tqdm(range(11)):
        sleep(1)
def two():
        for i in tqdm(range(11)):
            sleep(1)
def three():
        for i in tqdm(range(11)):
            sleep(1)

if __name__ == "__main__":
    t1 = threading.Thread(target=one)
    t2 = threading.Thread(target=two)
    t3 = threading.Thread(target=three)


    t1.start()
    t2.start()
    t3.start()

    t1.join()
    t2.join()
    t3.join()

After running the code in CMD I've seen this:

G:\>python thread2.py
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.01s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.01s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.01s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.01s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.01s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.02s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.02s/it]
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.01s/it]
G:\>


100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 11/11 [00:11<00:00,  1.02s/it]

I wanted only 3 progress bars but as you see, there are 9! How to avoid this problem?

Nameless
  • 147
  • 11
  • Does this answer your question? [how to use tqdm with multithreading?](https://stackoverflow.com/questions/63826035/how-to-use-tqdm-with-multithreading) – ti7 Oct 28 '21 at 17:38
  • Practically, writing to use a `concurrent.futures.ThreadPoolExecutor` is probably the best answer see other Question https://stackoverflow.com/questions/51601756/use-tqdm-with-concurrent-futures and Multiprocessing docs https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing.dummy – ti7 Oct 28 '21 at 18:00
  • @ti7 No actually.Problem still exist. – Nameless Nov 24 '21 at 09:29

0 Answers0