Parallel magic commands

IPython provides several magics for interactive use in parallel situations.

%px

The %px magic executes a single Python command on the engines specified by the targets attribute of the DirectView object.

Here we will tell all four engines to create 3x3 arrays and fill them with random numbers in [0, 1]:

In [25]: %px arrays = numpy.random.rand(3, 3)

Note that nothing is returned in this case. Each engine, however, has its own copy of the arrays object, and we can perform operations on it:

In [27]: %px numpy.linalg.eigvals(arrays)
Out[0:7]: array([ 1.49907907,  0.28987838,  0.49496096])
Out[1:7]: array([ 1.43756182,  0.27747814,  0.45153931])
Out[2:7]: array([ 1.51325036+0.j, -0.06614375+0.30396195j, -0.06614375-0.30396195j])
Out[3:7]: ...

Get Mastering IPython 4.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.