Last active
March 20, 2025 23:03
-
-
Save jczaplew/da809cfbdf0bf751ff520129a28e5467 to your computer and use it in GitHub Desktop.
tqdm with multithreading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tqdm import * | |
from queue import Queue | |
from threading import Thread | |
import time | |
THREADS = 4 | |
class WorkerThread(Thread): | |
def __init__(self, queue): | |
Thread.__init__(self) | |
self.queue = queue | |
def run(self): | |
while True: | |
pbar = self.queue.get() | |
time.sleep(2) | |
pbar.update() | |
self.queue.task_done() | |
queue = Queue() | |
# Create worker threads | |
for x in range(THREADS): | |
worker = WorkerThread(queue) | |
worker.daemon = True | |
worker.start() | |
pbar = tqdm(total=20) | |
for tile in range(20): | |
queue.put(pbar) | |
pbar.close() | |
queue.join() |
Same here
try using
from tqdm.auto import tqdm
To make this work, swap the last two lines - pbar.close()
must come after queue.join()
. You can only close the progress bar once the work is done!
Agree with @andrewlangemann - changing that order results in a functional tqdm
progress bar.
@jczaplew Would you be willing to update this gist with the fix?
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not working in console at least. An empty progress bar appears, remains that way and then after a while program exits.