Merge lp:~ipython-contrib/ipython/ipython-zmq into lp:ipython/0.11

Proposed by Fernando Perez
Status: Needs review
Proposed branch: lp:~ipython-contrib/ipython/ipython-zmq
Merge into: lp:ipython/0.11
Diff against target: 243 lines (+232/-0)
2 files modified
IPython/core/ipzmq.py (+174/-0)
IPython/kernel/ipkernelzmq.py (+58/-0)
To merge this branch: bzr merge lp:~ipython-contrib/ipython/ipython-zmq
Reviewer Review Type Date Requested Status
Fernando Perez Disapprove
Review via email: mp+24087@code.launchpad.net

Description of the change

Google Summer of Code branch proposal for the zmq port.

To post a comment you must log in.
1240. By Omar Andres Zapata Mesa <omazapa@tuxhome>

 implementing the server and client in zmq, this module
 init sockets to zmq conecction and let comunication between client and server.
 º

1241. By Omar Andres Zapata Mesa <omazapa@tuxhome>

Writing send and receive methods

Revision history for this message
Fernando Perez (fdo.perez) wrote :

I merged revisions 1237-1239 and cleaned the text up a little bit, but I did NOT merge 1240-41. The code in there will require further review, and we're now doing the move to github.

So once you start working on github, just remember to add the ipzmq.py file back to your git branch. But that code will need some fixes, which I can tell you about right away so you can start working on them:

- you are using 8 spaces, PEP 8 uses 4
- you are using camelCase, PEP 8 discourages that. Please read PEP 8 and adhere to it.
- do not use self.__X attributes with double underscore, they cause lots of problems in the long run. They are a mis-feature of python and should only be used in rare instances.

- Why aren't you using the code from the pyzmq examples as a starting point? That code already had much of what you are implementing here working...

So in summary: your docs have been merged (after fixing for capitalization and formatting, next time please be careful with that). The code isn't ready for merging.

review: Disapprove
1242. By Omar Andres Zapata Mesa <omazapa@tuxhome>

-> Corrections in namespacing, using pep8 instead camel case.
-> Implemented send and recieve in ipzmq but it have some errors
-> I started to write ipkernelzmq but it is not functional yet.

Unmerged revisions

1242. By Omar Andres Zapata Mesa <omazapa@tuxhome>

-> Corrections in namespacing, using pep8 instead camel case.
-> Implemented send and recieve in ipzmq but it have some errors
-> I started to write ipkernelzmq but it is not functional yet.

1241. By Omar Andres Zapata Mesa <omazapa@tuxhome>

Writing send and receive methods

1240. By Omar Andres Zapata Mesa <omazapa@tuxhome>

 implementing the server and client in zmq, this module
 init sockets to zmq conecction and let comunication between client and server.
 º

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'IPython/core/ipzmq.py'
2--- IPython/core/ipzmq.py 1970-01-01 00:00:00 +0000
3+++ IPython/core/ipzmq.py 2010-05-13 05:47:25 +0000
4@@ -0,0 +1,174 @@
5+#!/usr/bin/env python
6+# -*- coding: utf-8 -*-
7+"""A class to init a kernels server and client connector over 0MQ.
8+
9+Autor: Omar Andrés Zapata Mesa
10+ Fernando Perez
11+
12+Things to do:
13+-> sendMessage method
14+-> getMessage medthod
15+-> Message standards
16+-> json messages and compresion
17+
18+"""
19+
20+
21+import __builtin__
22+import sys
23+import time
24+import traceback
25+import zmq
26+from json import JSONEncoder, JSONDecoder
27+
28+json_encoder=JSONEncoder()
29+json_decoder=JSONDecoder()
30+
31+
32+class IpZmqServer(object):
33+ def __init__(self, ip='127.0.0.1', port_base=5555):
34+ """ Init defaults values to class """
35+ self._ip=ip
36+ self._port_base=port_base
37+
38+ self._Status=False
39+ #Context default Parameters zmq.Context(1,1,0)
40+ self._app_threads=1
41+ self._io_threads=1
42+ self._flags=0
43+
44+ #connection = ('tcp://%s' % ip) + ':%i'
45+ self._reply_connection= ('tcp://%s' % self._ip) + ':%i' %self._port_base
46+ self._public_connection= ('tcp://%s' % self._ip) + ':%i' %(self._port_base+1)
47+
48+ def set_context(self, app_threads=1, io_threads=1, flags=0):
49+ """ Write zmq context into class attributes."""
50+ self._app_threads = app_threads
51+ self._io_threads = io_threads
52+ self._flags = flags
53+
54+ def start(self):
55+ """ Init sockets connections to listen zmq request and send replies """
56+ self._Context = zmq.Context( self._app_threads, self._io_threads, self._flags)
57+
58+ self._reply_socket = self._Context.socket(zmq.XREP)
59+ try:
60+ self._reply_socket.bind(self._reply_connection)
61+ except:
62+ etype, evalue, tb = sys.exc_info()
63+ tb = traceback.format_exception(etype, evalue, tb)
64+
65+ self._public_socket = self._Context.socket(zmq.PUB)
66+ try:
67+ self._public_socket.bind( self._public_connection)
68+ except:
69+ etype, evalue, tb = sys.exc_info()
70+ tb = traceback.format_exception(etype, evalue, tb)
71+ self._Status=True
72+
73+ def stop(self):
74+ """Close all sockets and shutdown connections"""
75+ self._reply_socket = None
76+ self._public_socket = None
77+ self._Context = None
78+ self._Status == False
79+
80+ def get_public_socket(self):
81+ """ return public connection socket"""
82+ return self._public_socket
83+
84+ def get_reply_socket(self):
85+ """ return reply connection socket"""
86+ return self._reply_socket
87+
88+ def send(self,message):
89+ """Send messages through public socket
90+ message is a callable object or a string
91+ """
92+ if self._Status == False :
93+ raise ValueError('I/O operation: zmq server is stop')
94+ elif self._public_socket == None :
95+ raise ValueError('I/O operation on closed socked file')
96+ else:
97+ msg=json_encoder.encode(message)
98+ self._public_socket.send(msg)
99+
100+ def receive(self):
101+ """receive messages through reply socket """
102+ if self._Status == False :
103+ raise ValueError('I/O operation: zmq server is stop')
104+ elif self._reply_socket == None :
105+ raise ValueError('I/O operation on closed socked file')
106+ else:
107+ ident, msg = self._reply_socket.recv_json(True)
108+ msg=json_decoder.decode(msg)
109+ return ident, msg
110+
111+
112+class IpZmqClient(object):
113+ def __init__(self, ip='127.0.0.1', port_base=5555):
114+ """ Init defaults values to class, not init connections services, you need call start() method"""
115+ self._ip=ip
116+ self._port_base=port_base
117+
118+ #Context default Parameters zmq.Context(1,1,0)
119+ self._app_threads = 1
120+ self._io_threads = 1
121+ self._flags = 0
122+ self._Status = False
123+ self._request_connection = ('tcp://%s' % self._ip) + ':%i' %self._port_base
124+ self._sub_connection = ('tcp://%s' % self._ip) + ':%i' %(self._port_base+1)
125+
126+ def start(self):
127+ """ Init sockets connections to send zmq request and rec replies """
128+ self._Context = zmq.Context( self._app_threads, self._io_threads, self._flags)
129+ self._request_socket = self._Context.socket(zmq.XREQ)
130+ self._request_socket.connect(self._request_connection)
131+
132+ self._sub_socket = self._Context.socket(zmq.SUB)
133+ self._sub_socket.connect(self._sub_connection)
134+ self._sub_socket.setsockopt(zmq.SUBSCRIBE,'')
135+ self._Status=True
136+
137+ def stop(self):
138+ self._request_socket = None
139+ self._sub_socket = None
140+ self._Context = None
141+ self._Status = False
142+
143+
144+ def get_sub_socket(self):
145+ return self._sub_socket
146+
147+ def get_request_socket(self):
148+ return self._request_socket
149+
150+ def send(self,message):
151+ if self._Status == False :
152+ raise ValueError('I/O operation: zmq server is stop')
153+ elif self._sub_socket == None :
154+ raise ValueError('I/O operation on closed socked file')
155+ else:
156+ msg=json_encoder.encode(message)
157+ self._sub_socket.send(str(msg))
158+
159+ def receive(self):
160+ if self._Status == False :
161+ raise ValueError('I/O operation: zmq server is stop')
162+ elif self._request_socket == none :
163+ raise ValueError('I/O operation on closed socked file')
164+ else:
165+ ident, msg = self._request_socket.recv(True)
166+ msg=JSONDecoder.decode(msg)
167+ return ident, msg
168+
169+
170+if(__name__ == "__main__"):
171+ server=IpZmqServer("127.0.0.1",5555)
172+ server.start()
173+ client=IpZmqClient("127.0.0.1",5555)
174+ client.start()
175+ client.send("hi server!")
176+ server.stop()
177+ client.stop()
178+
179\ No newline at end of file
180
181=== added file 'IPython/kernel/ipkernelzmq.py'
182--- IPython/kernel/ipkernelzmq.py 1970-01-01 00:00:00 +0000
183+++ IPython/kernel/ipkernelzmq.py 2010-05-13 05:47:25 +0000
184@@ -0,0 +1,58 @@
185+# -*- coding: utf-8 -*-
186+# autors:
187+# Omar Andres Zapata Mesa
188+# Fernando Perez
189+
190+"""A interactive kernel that talks to a frontend over 0MQ.
191+
192+
193+
194+"""
195+
196+import __builtin__
197+import sys
198+import time
199+import traceback
200+
201+from code import CommandCompiler
202+
203+import zmq
204+
205+from IPython.core.ipzmq import IpZmqServer
206+from IPython.core.iplib import InteractiveShell
207+from IPython.utils.io import IOTerm
208+from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
209+from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap
210+from IPython.utils.io import Term
211+
212+from Queue import Queue
213+
214+
215+class IpKernelZmq(object):
216+ """ Ipython Zmq Kernel
217+ We start the server using zmq ipzmq, it will be waiting requests in the public socket
218+ so that it processes the ipython shell and put it in the output queue to be sent by the zmq`s reply socket.
219+ Ipython`s interactive shell will be running in a thread and input and output queues will be working asynchronously
220+
221+ """
222+ __init__(self, ip, port):
223+ self._input_queue=Queue(-1)
224+ self._output_queue=Queue(-1)
225+ self._ip_zmq_server=IpZmqServer(ip,port)
226+ self._shell=InteractiveShell()
227+ self._shell.init_alias()
228+ self._shell.init_builtins()
229+ self._shell.init_inspector()
230+ self._shell.init_magics()
231+ self._shell.init_pdb()
232+ self._shell.init_sys_modules()
233+ self._shell.init_instance_attrs()
234+ self._shell.init_user_ns()
235+ self._status = False
236+ def start(self):
237+ if !self._status :
238+ try:
239+ self._ip_zmq_server.start()
240+ except:
241+ else:
242+ print >> sys.stderr , "Kernel is now running"
243\ No newline at end of file

Subscribers

People subscribed via source and target branches