Last active
February 5, 2019 04:28
-
-
Save callistabee/26e9102b941d6a494d7e56b42e9c22dc to your computer and use it in GitHub Desktop.
it's multiprocessing.Pool.map, but with a progress bar widget.
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
import multiprocessing.pool as mp | |
import itertools | |
import ipywidgets | |
from IPython.display import display | |
class Pool(mp.Pool): | |
def map(self, func, iterable): | |
try: | |
nitems = len(iterable) | |
progress = ipywidgets.IntProgress( | |
value = 0, | |
min = 0, | |
max = nitems, | |
step = 1, | |
description = "0/%d" % nitems | |
) | |
nitems = str(nitems) | |
except TypeError: | |
progress = ipywidgets.IntProgress( | |
value = 1, | |
min = 0, | |
max = 1, | |
description = "0/?", | |
bar_style = 'info' | |
) | |
nitems = '?' | |
display(progress) | |
counter = itertools.count(1) | |
def update_progress(result): | |
count = next(counter) | |
progress.value = count | |
progress.description = "%d/%s" % (count, nitems) | |
promises = [] | |
for args in iterable: | |
try: | |
iter(args) | |
except TypeError: | |
args = (args,) | |
promises.append( | |
self.apply_async(func, args, callback=update_progress) | |
) | |
try: | |
return [promise.get() for promise in promises] | |
except: | |
progress.bar_style='danger' | |
raise | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment