Jupyter Snippet CB2nd 09_ipyparallel
Jupyter Snippet CB2nd 09_ipyparallel
5.9. Distributing Python code across multiple cores with IPython
from ipyparallel import Client
rc = Client()
rc.ids
[0, 1, 2, 3]
%%px
import os
print(f"Process {os.getpid():d}.")
[stdout:0] Process 10784.
[stdout:1] Process 10785.
[stdout:2] Process 10787.
[stdout:3] Process 10791.
%%px -t 1,2
# The os module has already been imported in
# the previous cell.
print(f"Process {os.getpid():d}.")
[stdout:1] Process 10785.
[stdout:2] Process 10787.
%%px -a
import time
time.sleep(5)
<AsyncResult: execute>
print(_.elapsed, _.ready())
1.522944 False
%pxresult
print(_.elapsed, _.ready())
5.044711 True
v = rc[:]
res = v.map(lambda x: x * x, range(10))
print(res.get())
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
with view.temp_flags(after=[arB, arC]):
arA = view.apply_async(f)