Jupyter Snippet CB2nd 10_async

Jupyter Snippet CB2nd 10_async

5.10. Interacting with asynchronous parallel tasks in IPython

import sys
import time
import ipyparallel
import ipywidgets
from IPython.display import clear_output, display
rc = ipyparallel.Client()
view = rc.load_balanced_view()
def f(x):
    import time
    time.sleep(.1)
    return x * x
numbers = list(range(100))
ar = view.map_async(f, numbers)
ar.metadata[0]
{'after': None,
 'completed': None,
 'data': {},
 ...
 'submitted': datetime.datetime(2017, ...)}
for i in ar:
    print(i, end=', ')
0, 1, 4, ..., 9801,
def progress_bar(ar):
    # We create a progress bar.
    w = ipywidgets.IntProgress()
    # The maximum value is the number of tasks.
    w.max = len(ar.msg_ids)
    # We display the widget in the output area.
    display(w)
    # Repeat:
    while not ar.ready():
        # Update the widget's value with the
        # number of tasks that have finished
        # so far.
        w.value = ar.progress
        time.sleep(.1)
    w.value = w.max
ar = view.map_async(f, numbers)
progress_bar(ar)

png