In [1]:
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 [2]:
from IPython.parallel import Client

c = Client(profile='default')
c


Out[2]:
<IPython.parallel.client.client.Client at 0x103eb5250>

Form views onto the cluster.


In [3]:
direct = c[:]

balanced = c.load_balanced_view()

In [4]:
direct.targets


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

Perform a parallel operation (apply)


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


Out[5]:
[18397, 18398, 18399, 18400]

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


Out[6]:
<AsyncResult: finished>

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


In [7]:
parallel_result.ready()


Out[7]:
True

In [8]:
parallel_result.result


Out[8]:
[18397, 18398, 18399, 18400]

Perform a parallel operation (map)


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


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

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


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

Maps can be asynchronous:


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


Out[11]:
<AsyncMapResult: finished>

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


In [12]:
parallel_result.ready()


Out[12]:
True

In [13]:
parallel_result.result


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

Make a remote function


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

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


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

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

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


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

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


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

In [ ]: