Created
February 12, 2018 21:20
-
-
Save hchasestevens/2320ea86ed31f0e22f517fb2ad05f4dc to your computer and use it in GitHub Desktop.
IPyParallel magic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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