Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Last active September 22, 2021 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jczaplew/da809cfbdf0bf751ff520129a28e5467 to your computer and use it in GitHub Desktop.
Save jczaplew/da809cfbdf0bf751ff520129a28e5467 to your computer and use it in GitHub Desktop.
tqdm with multithreading
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()
@parthi2929
Copy link

This is not working in console at least. An empty progress bar appears, remains that way and then after a while program exits.

@asyd
Copy link

asyd commented Apr 23, 2019

Same here

@miko3333
Copy link

try using
from tqdm.auto import tqdm

@andrewlangemann
Copy link

andrewlangemann commented Sep 22, 2021

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment