##// END OF EJS Templates
Work on updating parallel magic.
Brian Granger -
Show More
@@ -0,0 +1,189 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3
4 """Magic command interface for interactive parallel work."""
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 from IPython.core.component import Component
18 from IPython.utils.traitlets import Bool, Any
19 from IPython.utils.autoattr import auto_attr
20
21
22 #-------------------------------------------------------------------------------
23 # Definitions of magic functions for use with IPython
24 #-------------------------------------------------------------------------------
25
26 NO_ACTIVE_MULTIENGINE_CLIENT = """
27 Error: No Controller is activated
28 Use activate() on a MultiEngineClient object to activate it for magics.
29 """
30
31
32 class ParalleMagicComponent(Component):
33
34 active_multiengine_client = Any()
35 verbose = Bool(False, config=True)
36
37 def __init__(self, parent, name=None, config=None):
38 super(ParalleMagicComponent, self).__init__(parent, name=name, config=config)
39 self._define_magics()
40
41 # Access other components like this rather than by a regular attribute.
42 # This won't lookup the InteractiveShell object until it is used and
43 # then it is cached. This is both efficient and couples this class
44 # more loosely to InteractiveShell.
45 @auto_attr
46 def shell(self):
47 return Component.get_instances(
48 root=self.root,
49 klass='IPython.core.iplib.InteractiveShell')[0]
50
51 def _define_magics(self):
52 self.shell.define_magic('result', self.magic_result)
53 self.shell.define_magic('px', self.magic_px)
54 self.shell.define_magic('autopx', self.magix_autopx)
55
56 def magic_result(self, ipself, parameter_s=''):
57 """Print the result of command i on all engines of the active controller.
58
59 To activate a controller in IPython, first create it and then call
60 the activate() method.
61
62 Then you can do the following:
63
64 >>> result # Print the latest result
65 Printing result...
66 [127.0.0.1:0] In [1]: b = 10
67 [127.0.0.1:1] In [1]: b = 10
68
69 >>> result 0 # Print result 0
70 In [14]: result 0
71 Printing result...
72 [127.0.0.1:0] In [0]: a = 5
73 [127.0.0.1:1] In [0]: a = 5
74 """
75 if self.active_multiengine_client is None:
76 print NO_ACTIVE_MULTIENGINE_CLIENT
77 return
78
79 try:
80 index = int(parameter_s)
81 except:
82 index = None
83 result = self.active_multiengine_client.get_result(index)
84 return result
85
86 # def magic_px(self,parameter_s=''):
87 # """Executes the given python command on the active IPython Controller.
88 #
89 # To activate a Controller in IPython, first create it and then call
90 # the activate() method.
91 #
92 # Then you can do the following:
93 #
94 # >>> %px a = 5 # Runs a = 5 on all nodes
95 # """
96 #
97 # try:
98 # activeController = __IPYTHON__.activeController
99 # except AttributeError:
100 # print NO_ACTIVE_CONTROLLER
101 # else:
102 # print "Parallel execution on engines: %s" % activeController.targets
103 # result = activeController.execute(parameter_s)
104 # return result
105 #
106 # def pxrunsource(self, source, filename="<input>", symbol="single"):
107 #
108 # try:
109 # code = self.compile(source, filename, symbol)
110 # except (OverflowError, SyntaxError, ValueError):
111 # # Case 1
112 # self.showsyntaxerror(filename)
113 # return None
114 #
115 # if code is None:
116 # # Case 2
117 # return True
118 #
119 # # Case 3
120 # # Because autopx is enabled, we now call executeAll or disable autopx if
121 # # %autopx or autopx has been called
122 # if 'get_ipython().magic("%autopx' in source or 'get_ipython().magic("autopx' in source:
123 # _disable_autopx(self)
124 # return False
125 # else:
126 # try:
127 # result = self.activeController.execute(source)
128 # except:
129 # self.showtraceback()
130 # else:
131 # print result.__repr__()
132 # return False
133 #
134 # def magic_autopx(self, parameter_s=''):
135 # """Toggles auto parallel mode for the active IPython Controller.
136 #
137 # To activate a Controller in IPython, first create it and then call
138 # the activate() method.
139 #
140 # Then you can do the following:
141 #
142 # >>> %autopx # Now all commands are executed in parallel
143 # Auto Parallel Enabled
144 # Type %autopx to disable
145 # ...
146 # >>> %autopx # Now all commands are locally executed
147 # Auto Parallel Disabled
148 # """
149 #
150 # if hasattr(self, 'autopx'):
151 # if self.autopx == True:
152 # _disable_autopx(self)
153 # else:
154 # _enable_autopx(self)
155 # else:
156 # _enable_autopx(self)
157 #
158 # def _enable_autopx(self):
159 # """Enable %autopx mode by saving the original runsource and installing
160 # pxrunsource.
161 # """
162 # try:
163 # activeController = __IPYTHON__.activeController
164 # except AttributeError:
165 # print "No active RemoteController found, use RemoteController.activate()."
166 # else:
167 # self._original_runsource = self.runsource
168 # self.runsource = new.instancemethod(pxrunsource, self, self.__class__)
169 # self.autopx = True
170 # print "Auto Parallel Enabled\nType %autopx to disable"
171 #
172 # def _disable_autopx(self):
173 # """Disable %autopx by restoring the original runsource."""
174 # if hasattr(self, 'autopx'):
175 # if self.autopx == True:
176 # self.runsource = self._original_runsource
177 # self.autopx = False
178 # print "Auto Parallel Disabled"
179
180
181 _loaded = False
182
183 def load_ipython_extension(ip):
184 """Load the extension in IPython as a hook."""
185 global _loaded
186 if not _loaded:
187 prd = ParalleMagicComponent(ip, name='parallel_magic')
188 _loaded = True
189
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now