from IPython.parallel import Client rc = Client() rc.ids dview = rc[:] with open('../builtin-cpuheavy/prime_list.txt') as f: PRIMES = [int(l) for l in f] def is_prime(n): import math # import until the function is called # make sure all engines import math if n % 2 == 0: return False sqrt_n = int(math.floor(math.sqrt(n))) for i in range(3, sqrt_n + 1, 2): if n % i == 0: return False return True ar = dview.map_async(is_prime, PRIMES[:8]) ar.wait_interactive() # wait blocks, and wait_interactive provide working status ar.get() speedup = ar.serial_time / ar.wall_time speedup ar.metadata[:1] with dview.sync_imports(): import math import numpy as np def find_np(): np.random.randint(10) rc[:2].apply_sync(find_np) %%px import numpy as np np.random.randint(6) %%px # try to run it multiple times, engines use same processes (like a remote Python intepreter) import os os.getpid() # push dview['prog'] = 'val_prime' # pull dview['prog'] # all engines get x but with differnt value ar = dview.scatter('x', list(range(13))) ar.wait() dview['x'] # get x from all engines and combined dview.gather('x', block=True) %%px import numpy as np rand_n = np.random.randint(0, 10, 6) dview['rand_n'] dview.gather('rand_n', block=True) # sum at each engine def rand_sum(): return np.sum(rand_n) ar = dview.apply_sync(rand_sum) ar.get() # parallel sum shoud equal to serial sum sum(ar.get()) == sum(dview.gather('rand_n', block=True))