Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Last active March 24, 2017 06:22
Show Gist options
  • Save belltailjp/5253066638ec76d97b085172b51356b3 to your computer and use it in GitHub Desktop.
Save belltailjp/5253066638ec76d97b085172b51356b3 to your computer and use it in GitHub Desktop.
A dirty hack to make tqdm process safe http://daily.belltail.jp/?p=2389
import tqdm
import multiprocessing
class ProcessSafeTqdm(tqdm.tqdm):
def __init__(self, *args):
super(ProcessSafeTqdm, self).__init__(*args)
self.correct_count = multiprocessing.Value('i', 0)
def update(self, n=1):
with self.correct_count.get_lock():
self.n = self.correct_count.value
super(ProcessSafeTqdm, self).update(n)
self.correct_count.value += n
def close(self):
self.n = self.correct_count.value
super(ProcessSafeTqdm, self).close()
import sys
import time
import tqdm
import multiprocessing
from process_safe_tqdm import ProcessSafeTqdm
def worker(pbar):
pbar.update()
ar = list(range(100))
#with tqdm.tqdm(ar) as pbar: # It doesn't produce meaningful progress bar
with ProcessSafeTqdm(ar) as pbar:
jobs = []
for t in ar:
job = multiprocessing.Process(target=worker, args=(pbar,))
job.start()
jobs.append(job)
for job in jobs:
job.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment