Show More
@@ -0,0 +1,260 b'' | |||||
|
1 | .. _ipythonzmq | |||
|
2 | ||||
|
3 | =================================================== | |||
|
4 | porting ipython to a two process model using zeromq | |||
|
5 | =================================================== | |||
|
6 | ||||
|
7 | abstract | |||
|
8 | ---------- | |||
|
9 | ||||
|
10 | ipython's execution in a command-line environment will be ported to a two process model using the zeromq library for inter-process communication. this will: | |||
|
11 | ||||
|
12 | - prevent an interpreter crash from destroying the user session, | |||
|
13 | - allow multiple clients to interact simultaneously with a single interpreter | |||
|
14 | - allow ipython to reuse code for local execution and distributed computing (dc) | |||
|
15 | - give us a path for python3 support, since zeromq supports python3 while twisted (what we use today for dc) does not. | |||
|
16 | ||||
|
17 | deliverables | |||
|
18 | ---------------- | |||
|
19 | ||||
|
20 | * a user-facing frontend that provides an environment like today's command-line ipython but running over two processes, with the code execution kernel living in a separate process and communicating with the frontend by using the zeromq library. | |||
|
21 | ||||
|
22 | * a kernel that supports ipython's features (tab-completion, code introspection, exception reporting with different levels of detail, etc), but listening to requests over a network port, and returning results as json-formatted messages over the network. | |||
|
23 | ||||
|
24 | ||||
|
25 | project description | |||
|
26 | --------------------- | |||
|
27 | ||||
|
28 | currently ipython provides a command-line client that executes all code in a single process, and a set of tools for distributed and parallel computing that execute code in multiple processes (possibly but not necessarily on different hosts), using the twisted asynchronous framework for communication between nodes. for a number of reasons, it is desirable to unify the architecture of the local execution with that of distributed computing, since ultimately many of the underlying abstractions are similar and should be reused. in particular, we would like to: | |||
|
29 | ||||
|
30 | - have even for a single user a 2-process model, so that the environment where code is being input runs in a different process from that which executes the code. this would prevent a crash of the python interpreter executing code (because of a segmentation fault in a compiled extension or an improper access to a c library via ctypes, for example) from destroying the user session. | |||
|
31 | ||||
|
32 | - have the same kernel used for executing code locally be available over the network for distributed computing. currently the twisted-using ipython engines for distributed computing do not share any code with the command-line client, which means that many of the additional features of ipython (tab completion, object introspection, magic functions, etc) are not available while using the distributed computing system. once the regular command-line environment is ported to allowing such a 2-process model, this newly decoupled kernel could form the core of a distributed computing ipython engine and all capabilities would be available throughout the system. | |||
|
33 | ||||
|
34 | - have a route to python3 support. twisted is a large and complex library that does currently not support python3, and as indicated by the twisted developers it may take a while before it is ported (http://stackoverflow.com/questions/172306/how-are-you-planning-on-handling-the-migration-to-python-3). for ipython, this means that while we could port the command-line environment, a large swath of ipython would be left 2.x-only, a highly undesirable situation. for this reason, the search for an alternative to twisted has been active for a while, and recently we've identified the zeromq (http://www.zeromq.org, zmq for short) library as a viable candidate. zmq is a fast, simple messaging library written in c++, for which one of the ipython developers has written python bindings using cython (http://www.zeromq.org/bindings:python). since cython already knows how to generate python3-compliant bindings with a simple command-line switch, zmq can be used with python3 when needed. | |||
|
35 | ||||
|
36 | as part of the zmq python bindings, the ipython developers have already developed a simple prototype of such a two-process kernel/frontend system (details below). i propose to start from this example and port today's ipython code to operate in a similar manner. ipython's command-line program (the main 'ipython' script) executes both user interaction and the user's code in the same process. this project will thus require breaking up ipython into the parts that correspond to the kernel and the parts that are meant to interact with the user, and making these two components communicate over the network using zmq instead of accessing local attributes and methods of a single global object. | |||
|
37 | ||||
|
38 | once this port is complete, the resulting tools will be the foundation (though as part of this proposal i do not expect to undertake either of these tasks) to allow the distributed computing parts of ipython to use the same code as the command-line client, and for the whole system to be ported to python3. so while i do not intend to tackle here the removal of twisted and the unification of the local and distributed parts of ipython, my proposal is a necessary step before those are possible. | |||
|
39 | ||||
|
40 | project details | |||
|
41 | ------------------- | |||
|
42 | ||||
|
43 | As part of the zeromq bindings, the ipython developers have already developed a simple prototype example that provides a python execution kernel (with none of ipython's code or features, just plain code execution) that listens on zmq sockets, and a frontend based on the interactiveconsole class of the code.py module from the python standard library. this example is capable of executing code, propagating errors, performing tab-completion over the network and having multiple frontends connect and disconnect simultaneously to a single kernel, with all inputs and outputs being made available to all connected clients (thanks to zqm's pub sockets that provide multicasting capabilities for the kernel and to which the frontends subscribe via a sub socket). | |||
|
44 | ||||
|
45 | this simple code shows how the kernel starts: | |||
|
46 | ||||
|
47 | +----------------------------------------------------------------------------+ | |||
|
48 | | def main(): | | |||
|
49 | | | | |||
|
50 | | c = zmq.context(1, 1) | | |||
|
51 | | | | |||
|
52 | | #ip = '192.168.2.109' | | |||
|
53 | | | | |||
|
54 | | ip = '127.0.0.1' | | |||
|
55 | | | | |||
|
56 | | #ip = '192.168.4.128' | | |||
|
57 | | | | |||
|
58 | | port_base = 5555 | | |||
|
59 | | | | |||
|
60 | | connection = ('tcp://%s' % ip) + ':%i' | | |||
|
61 | | | | |||
|
62 | | rep_conn = connection % port_base | | |||
|
63 | | | | |||
|
64 | | pub_conn = connection % (port_base+1) | | |||
|
65 | | | | |||
|
66 | | print >>sys.__stdout__, "starting the kernel..." | | |||
|
67 | | | | |||
|
68 | | print >>sys.__stdout__, "on:",rep_conn, pub_conn | | |||
|
69 | | | | |||
|
70 | | session = session(username=u'kernel') | | |||
|
71 | | | | |||
|
72 | | reply_socket = c.socket(zmq.xrep) | | |||
|
73 | | | | |||
|
74 | | reply_socket.bind(rep_conn) | | |||
|
75 | | | | |||
|
76 | | pub_socket = c.socket(zmq.pub) | | |||
|
77 | | | | |||
|
78 | | pub_socket.bind(pub_conn) | | |||
|
79 | | | | |||
|
80 | | stdout = outstream(session, pub_socket, u'stdout') | | |||
|
81 | | | | |||
|
82 | | stderr = outstream(session, pub_socket, u'stderr') | | |||
|
83 | | | | |||
|
84 | | sys.stdout = stdout | | |||
|
85 | | | | |||
|
86 | | sys.stderr = stderr | | |||
|
87 | | | | |||
|
88 | | display_hook = displayhook(session, pub_socket) | | |||
|
89 | | | | |||
|
90 | | sys.displayhook = display_hook | | |||
|
91 | | | | |||
|
92 | | kernel = kernel(session, reply_socket, pub_socket) | | |||
|
93 | | | | |||
|
94 | +----------------------------------------------------------------------------+ | |||
|
95 | ||||
|
96 | ||||
|
97 | and the heart of the kernel is the method to execute code and post back the results over the necessary sockets, using json-formatted messages: | |||
|
98 | ||||
|
99 | +-----------------------------------------------------------------------------+ | |||
|
100 | | def execute_request(self, ident, parent): | | |||
|
101 | | | | |||
|
102 | | try: | | |||
|
103 | | | | |||
|
104 | | code = parent[u'content'][u'code'] | | |||
|
105 | | | | |||
|
106 | | except: | | |||
|
107 | | | | |||
|
108 | | print>>sys.__stderr__, "got bad msg: " | | |||
|
109 | | | | |||
|
110 | | print>>sys.__stderr__, message(parent) | | |||
|
111 | | | | |||
|
112 | | return | | |||
|
113 | | | | |||
|
114 | | pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) | | |||
|
115 | | | | |||
|
116 | | self.pub_socket.send_json(pyin_msg) | | |||
|
117 | | | | |||
|
118 | | try: | | |||
|
119 | | | | |||
|
120 | | comp_code = self.compiler(code, '<zmq-kernel>') | | |||
|
121 | | | | |||
|
122 | | sys.displayhook.set_parent(parent) | | |||
|
123 | | | | |||
|
124 | | exec comp_code in self.user_ns, self.user_ns | | |||
|
125 | | | | |||
|
126 | | except: | | |||
|
127 | | | | |||
|
128 | | result = u'error' | | |||
|
129 | | | | |||
|
130 | | etype, evalue, tb = sys.exc_info() | | |||
|
131 | | | | |||
|
132 | | tb = traceback.format_exception(etype, evalue, tb) | | |||
|
133 | | | | |||
|
134 | | exc_content = { | | |||
|
135 | | | | |||
|
136 | | u'status' : u'error', | | |||
|
137 | | | | |||
|
138 | | u'traceback' : tb, | | |||
|
139 | | | | |||
|
140 | | u'etype' : unicode(etype), | | |||
|
141 | | | | |||
|
142 | | u'evalue' : unicode(evalue) | | |||
|
143 | | | | |||
|
144 | | } | | |||
|
145 | | | | |||
|
146 | | exc_msg = self.session.msg(u'pyerr', exc_content, parent) | | |||
|
147 | | | | |||
|
148 | | self.pub_socket.send_json(exc_msg) | | |||
|
149 | | | | |||
|
150 | | reply_content = exc_content | | |||
|
151 | | | | |||
|
152 | | else: | | |||
|
153 | | | | |||
|
154 | | reply_content = {'status' : 'ok'} | | |||
|
155 | | | | |||
|
156 | | reply_msg = self.session.msg(u'execute_reply', reply_content, parent)| | |||
|
157 | | | | |||
|
158 | | print>>sys.__stdout__, message(reply_msg) | | |||
|
159 | | | | |||
|
160 | | self.reply_socket.send_json(reply_msg, ident=ident) | | |||
|
161 | | | | |||
|
162 | | if reply_msg['content']['status'] == u'error': | | |||
|
163 | | | | |||
|
164 | | self.abort_queue() | | |||
|
165 | | | | |||
|
166 | | | | |||
|
167 | +-----------------------------------------------------------------------------+ | |||
|
168 | ||||
|
169 | and a little system to tab-completion | |||
|
170 | ||||
|
171 | +-----------------------------------------------------------------------------+ | |||
|
172 | | def execute_request(self, ident, parent): | | |||
|
173 | | | | |||
|
174 | | try: | | |||
|
175 | | | | |||
|
176 | | code = parent[u'content'][u'code'] | | |||
|
177 | | | | |||
|
178 | | except: | | |||
|
179 | | | | |||
|
180 | | print>>sys.__stderr__, "got bad msg: " | | |||
|
181 | | | | |||
|
182 | | print>>sys.__stderr__, message(parent) | | |||
|
183 | | | | |||
|
184 | | return | | |||
|
185 | | | | |||
|
186 | | pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent) | | |||
|
187 | | | | |||
|
188 | | self.pub_socket.send_json(pyin_msg) | | |||
|
189 | | | | |||
|
190 | | try: | | |||
|
191 | | | | |||
|
192 | | comp_code = self.compiler(code, '<zmq-kernel>') | | |||
|
193 | | | | |||
|
194 | | sys.displayhook.set_parent(parent) | | |||
|
195 | | | | |||
|
196 | | exec comp_code in self.user_ns, self.user_ns | | |||
|
197 | | | | |||
|
198 | | except: | | |||
|
199 | | | | |||
|
200 | | result = u'error' | | |||
|
201 | | | | |||
|
202 | | etype, evalue, tb = sys.exc_info() | | |||
|
203 | | | | |||
|
204 | | tb = traceback.format_exception(etype, evalue, tb) | | |||
|
205 | | | | |||
|
206 | | exc_content = { | | |||
|
207 | | | | |||
|
208 | | u'status' : u'error', | | |||
|
209 | | | | |||
|
210 | | u'traceback' : tb, | | |||
|
211 | | | | |||
|
212 | | u'etype' : unicode(etype), | | |||
|
213 | | | | |||
|
214 | | u'evalue' : unicode(evalue) | | |||
|
215 | | | | |||
|
216 | | } | | |||
|
217 | | | | |||
|
218 | | exc_msg = self.session.msg(u'pyerr', exc_content, parent) | | |||
|
219 | | | | |||
|
220 | | self.pub_socket.send_json(exc_msg) | | |||
|
221 | | | | |||
|
222 | | reply_content = exc_content | | |||
|
223 | | | | |||
|
224 | | else: | | |||
|
225 | | | | |||
|
226 | | reply_content = {'status' : 'ok'} | | |||
|
227 | | | | |||
|
228 | | reply_msg = self.session.msg(u'execute_reply', reply_content, parent)| | |||
|
229 | | | | |||
|
230 | | print>>sys.__stdout__, message(reply_msg) | | |||
|
231 | | | | |||
|
232 | | self.reply_socket.send_json(reply_msg, ident=ident) | | |||
|
233 | | | | |||
|
234 | | if reply_msg['content']['status'] == u'error': | | |||
|
235 | | | | |||
|
236 | | self.abort_queue() | | |||
|
237 | | | | |||
|
238 | +-----------------------------------------------------------------------------+ | |||
|
239 | ||||
|
240 | ||||
|
241 | ||||
|
242 | ||||
|
243 | ** we have all example code in | |||
|
244 | ||||
|
245 | * http://github.com/ellisonbg/pyzmq/blob/completer/examples/kernel/kernel.py | |||
|
246 | ||||
|
247 | * http://github.com/ellisonbg/pyzmq/blob/completer/examples/kernel/completer.py | |||
|
248 | ||||
|
249 | * http://github.com/fperez/pyzmq/blob/completer/examples/kernel/frontend.py | |||
|
250 | ||||
|
251 | ||||
|
252 | all of this code already works, and can be seen in this example directory from the zmq python bindings: | |||
|
253 | ||||
|
254 | * http://github.com/ellisonbg/pyzmq/blob/completer/examples/kernel | |||
|
255 | ||||
|
256 | ||||
|
257 | based on this work, i expect to write a stable system for ipython kernel with ipython standards, error control,crash recovery system and general configuration options, also standardize defaults ports or auth system for remote connection etc. | |||
|
258 | ||||
|
259 | the crash recovery system, is a ipython kernel module for when it fails unexpectedly, you can retrieve the information from the section, this will be based on a log and a lock file to indicate when the kernel was not closed in a proper way. | |||
|
260 |
@@ -1,20 +1,21 b'' | |||||
1 | .. _developer_guide: |
|
1 | .. _developer_guide: | |
2 |
|
2 | |||
3 | ========================= |
|
3 | ========================= | |
4 | IPython developer's guide |
|
4 | IPython developer's guide | |
5 | ========================= |
|
5 | ========================= | |
6 |
|
6 | |||
7 | .. toctree:: |
|
7 | .. toctree:: | |
8 | :maxdepth: 1 |
|
8 | :maxdepth: 1 | |
9 |
|
9 | |||
10 | contributing.txt |
|
10 | contributing.txt | |
11 | coding_guide.txt |
|
11 | coding_guide.txt | |
12 | doc_guide.txt |
|
12 | doc_guide.txt | |
13 | testing.txt |
|
13 | testing.txt | |
14 | release.txt |
|
14 | release.txt | |
15 | roadmap.txt |
|
15 | roadmap.txt | |
16 | reorg.txt |
|
16 | reorg.txt | |
17 | magic_blueprint.txt |
|
17 | magic_blueprint.txt | |
18 | notification_blueprint.txt |
|
18 | notification_blueprint.txt | |
19 | ipgraph.txt |
|
19 | ipgraph.txt | |
|
20 | ipythonzmq.txt | |||
20 |
|
21 |
General Comments 0
You need to be logged in to leave comments.
Login now