In [22]:
import os

Start the ipython cluster:

In a terminal you can type:

ipcluster start --profile=default --n=4

You can also use the IPython web client.


In [23]:
from IPython.parallel import Client

c = Client(profile='default')
c


Out[23]:
<IPython.parallel.client.client.Client at 0x105acc050>

Form views onto the cluster.


In [24]:
direct = c[:]

balanced = c.load_balanced_view()

In [25]:
direct.targets


Out[25]:
[0, 1, 2, 3]

Perform a parallel operation (apply)


In [26]:
parallel_result = direct.apply_sync(os.getpid)
parallel_result


Out[26]:
[32720, 32721, 32722, 32723]

In [27]:
parallel_result = direct.apply(os.getpid)
parallel_result


Out[27]:
<AsyncResult: finished>

AsyncResult: A class for representing results of non-blocking calls.


In [28]:
parallel_result.ready()


Out[28]:
True

In [29]:
parallel_result.result


Out[29]:
[32720, 32721, 32722, 32723]

Perform a parallel operation (map)


In [30]:
serial_result = list(map(lambda x:x**10, range(4)))
serial_result


Out[30]:
[0, 1, 1024, 59049]

In [31]:
parallel_result = list(direct.map(lambda x:x**10, range(4)))
parallel_result


Out[31]:
[0, 1, 1024, 59049]

Maps can be asynchronous:


In [32]:
parallel_result = direct.map_async(lambda x:x**10, range(4))
parallel_result


Out[32]:
<AsyncMapResult: finished>

AsyncMapResult: A class for representing results of non-blocking gathers.


In [33]:
parallel_result.ready()


Out[33]:
True

In [34]:
parallel_result.result


Out[34]:
[0, 1, 1024, 59049]

Make a remote function


In [35]:
def power(a, power=10):
    return a**power

In [36]:
[power(x) for x in range(4)]


Out[36]:
[0, 1, 1024, 59049]

In [37]:
@balanced.remote()
def power(a, power=10):
    return a**power

In [38]:
[power(x) for x in range(4)]


Out[38]:
[<AsyncResult: power>,
 <AsyncResult: power>,
 <AsyncResult: power>,
 <AsyncResult: power>]

In [39]:
[power(x).result for x in range(4)]


Out[39]:
[0, 1, 1024, 59049]

In [39]: