Skip to content

Instantly share code, notes, and snippets.

@hervenivon
Created March 13, 2019 09:12
Show Gist options
  • Save hervenivon/e283e275127a2c5404fb2b5dfb4ea766 to your computer and use it in GitHub Desktop.
Save hervenivon/e283e275127a2c5404fb2b5dfb4ea766 to your computer and use it in GitHub Desktop.
Scaffolding for parallel python and `tqdm`
#!/usr/bin/env python3
from multiprocessing import Pool
from tqdm import tqdm
from queue import Queue
from threading import Thread
import random
import time
THREADS = 5
NBPROCESS = 5
NBELTS = 20
class WorkerThread(Thread):
def __init__(self, queue, pbar):
Thread.__init__(self)
self.queue = queue
self.pbar = pbar
def run(self):
while True:
pbar = self.queue.get()
time.sleep(random.uniform(0.5,4.0))
self.pbar.update(1)
self.pbar.refresh()
self.queue.task_done()
def RunParallelThreads():
queue = Queue()
pbar = tqdm(total=NBELTS, miniters=1)
# Create worker threads
for x in range(THREADS):
worker = WorkerThread(queue, pbar)
worker.daemon = True
worker.start()
for i in range(NBELTS):
queue.put(i)
queue.join()
pbar.close()
def exe(elt):
time.sleep(random.uniform(0.5,4.0))
def RunParallelPool():
pool = Pool(processes=NBPROCESS)
with tqdm(total=NBELTS) as t:
for _ in pool.imap_unordered(exe, range(NBELTS)):
t.update(1)
if __name__ == '__main__':
RunParallelThreads()
RunParallelPool()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment