Skip to content

Instantly share code, notes, and snippets.

@hchasestevens
Created February 12, 2018 21:20
Show Gist options
  • Save hchasestevens/2320ea86ed31f0e22f517fb2ad05f4dc to your computer and use it in GitHub Desktop.
Save hchasestevens/2320ea86ed31f0e22f517fb2ad05f4dc to your computer and use it in GitHub Desktop.
IPyParallel magic
from ipyparallel import Client, require
from IPython.core.magic import Magics, magics_class, cell_magic, line_magic
RC = Client()
@contextmanager
def apply_parallel(block=False, remote_client=RC):
view = remote_client.load_balanced_view()
async_results = []
def _apply_async(*args, **kwargs):
async_result = view.apply_async(*args, **kwargs)
async_results.append(async_result)
return async_result
yield _apply_async
if block:
remote_client.wait(async_results)
@magics_class
class ParallelMagics(Magics):
@line_magic
def pxcells(self, line):
RC[:].execute('\n'.join(
self.shell.find_user_code(group)
for group in line.split('#')[0].strip().split(',')
))
get_ipython().register_magics(ParallelMagics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment