joblib for parallel processing


In [7]:
from joblib import Parallel, delayed
import random
import time

In [11]:
def f(x):
    time.sleep(1)
    return x ** 2

In [12]:
%%time
Parallel(n_jobs=1)(delayed(f)(i) for i in range(10))


CPU times: user 2.85 ms, sys: 1.28 ms, total: 4.13 ms
Wall time: 10 s
Out[12]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [13]:
%%time
Parallel(n_jobs=8)(delayed(f)(i) for i in range(10))


CPU times: user 27.5 ms, sys: 28.5 ms, total: 56 ms
Wall time: 2.12 s
Out[13]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [14]:
common_thing = 0

def f(x):
    common_thing+= x

In [15]:
%%time
# This doesn't work
Parallel(n_jobs=1)(delayed(f)(i) for i in range(10))


---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-15-07f397721c36> in <module>()
----> 1 get_ipython().run_cell_magic('time', '', 'Parallel(n_jobs=1)(delayed(f)(i) for i in range(10))')

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2118             magic_arg_s = self.var_expand(line, stack_depth)
   2119             with self.builtin_trap:
-> 2120                 result = fn(magic_arg_s, cell)
   2121             return result
   2122 

<decorator-gen-60> in time(self, line, cell, local_ns)

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/IPython/core/magics/execution.py in time(self, line, cell, local_ns)
   1171         if mode=='eval':
   1172             st = clock2()
-> 1173             out = eval(code, glob, local_ns)
   1174             end = clock2()
   1175         else:

<timed eval> in <module>()

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/joblib/parallel.py in __call__(self, iterable)
    777             # was dispatched. In particular this covers the edge
    778             # case of Parallel used with an exhausted iterator.
--> 779             while self.dispatch_one_batch(iterator):
    780                 self._iterating = True
    781             else:

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/joblib/parallel.py in dispatch_one_batch(self, iterator)
    623                 return False
    624             else:
--> 625                 self._dispatch(tasks)
    626                 return True
    627 

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/joblib/parallel.py in _dispatch(self, batch)
    586         dispatch_timestamp = time.time()
    587         cb = BatchCompletionCallBack(dispatch_timestamp, len(batch), self)
--> 588         job = self._backend.apply_async(batch, callback=cb)
    589         self._jobs.append(job)
    590 

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/joblib/_parallel_backends.py in apply_async(self, func, callback)
    109     def apply_async(self, func, callback=None):
    110         """Schedule a func to be run"""
--> 111         result = ImmediateResult(func)
    112         if callback:
    113             callback(result)

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/joblib/_parallel_backends.py in __init__(self, batch)
    330         # Don't delay the application, to avoid keeping the input
    331         # arguments in memory
--> 332         self.results = batch()
    333 
    334     def get(self):

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/joblib/parallel.py in __call__(self)
    129 
    130     def __call__(self):
--> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
    132 
    133     def __len__(self):

/Users/amir.ziai/anaconda/lib/python3.5/site-packages/joblib/parallel.py in <listcomp>(.0)
    129 
    130     def __call__(self):
--> 131         return [func(*args, **kwargs) for func, args, kwargs in self.items]
    132 
    133     def __len__(self):

<ipython-input-14-94dc4d012467> in f(x)
      2 
      3 def f(x):
----> 4     common_thing+= x

UnboundLocalError: local variable 'common_thing' referenced before assignment