##// END OF EJS Templates
Cleaned up KernelManager interface and clarified documentation.
epatters -
Show More
@@ -66,10 +66,9 b" if __name__ == '__main__':"
66 from IPython.frontend.qt.kernelmanager import QtKernelManager
66 from IPython.frontend.qt.kernelmanager import QtKernelManager
67
67
68 # Create KernelManager
68 # Create KernelManager
69 xreq_addr = ('127.0.0.1', 5575)
69 kernel_manager = QtKernelManager(xreq_address = ('127.0.0.1', 5575),
70 sub_addr = ('127.0.0.1', 5576)
70 sub_address = ('127.0.0.1', 5576),
71 rep_addr = ('127.0.0.1', 5577)
71 rep_address = ('127.0.0.1', 5577))
72 kernel_manager = QtKernelManager(xreq_addr, sub_addr, rep_addr)
73 kernel_manager.sub_channel.start()
72 kernel_manager.sub_channel.start()
74 kernel_manager.xreq_channel.start()
73 kernel_manager.xreq_channel.start()
75
74
@@ -25,13 +25,15 b' class MissingHandlerError(Exception):'
25
25
26
26
27 class ZmqSocketChannel(Thread):
27 class ZmqSocketChannel(Thread):
28 """ The base class for the channels that use ZMQ sockets.
29 """
28
30
29 socket = None
31 def __init__(self, context, session, addr=None):
30
31 def __init__(self, context, session, addr):
32 self.context = context
32 self.context = context
33 self.session = session
33 self.session = session
34 self.addr = addr
34 self.addr = addr
35 self.socket = None
36
35 super(ZmqSocketChannel, self).__init__()
37 super(ZmqSocketChannel, self).__init__()
36 self.daemon = True
38 self.daemon = True
37
39
@@ -41,7 +43,7 b' class SubSocketChannel(ZmqSocketChannel):'
41 handlers = None
43 handlers = None
42 _overriden_call_handler = None
44 _overriden_call_handler = None
43
45
44 def __init__(self, context, session, addr):
46 def __init__(self, context, session, addr=None):
45 self.handlers = {}
47 self.handlers = {}
46 super(SubSocketChannel, self).__init__(context, session, addr)
48 super(SubSocketChannel, self).__init__(context, session, addr)
47
49
@@ -134,7 +136,7 b' class XReqSocketChannel(ZmqSocketChannel):'
134 handlers = None
136 handlers = None
135 _overriden_call_handler = None
137 _overriden_call_handler = None
136
138
137 def __init__(self, context, session, addr):
139 def __init__(self, context, session, addr=None):
138 self.handlers = {}
140 self.handlers = {}
139 self.handler_queue = Queue()
141 self.handler_queue = Queue()
140 self.command_queue = Queue()
142 self.command_queue = Queue()
@@ -249,16 +251,16 b' class RepSocketChannel(ZmqSocketChannel):'
249
251
250
252
251 class KernelManager(HasTraits):
253 class KernelManager(HasTraits):
254 """ Manages a kernel for a frontend.
252
255
253 # The addresses to use for the various channels. Should be tuples of form
256 The SUB channel is for the frontend to receive messages published by the
254 # (ip_address, port).
257 kernel.
255 sub_address = Any
258
256 xreq_address = Any
259 The REQ channel is for the frontend to make requests of the kernel.
257 rep_address = Any
260
258 # FIXME: Add Tuple to Traitlets.
261 The REP channel is for the kernel to request stdin (raw_input) from the
259 #sub_address = Tuple(Str, Int)
262 frontend.
260 #xreq_address = Tuple(Str, Int)
263 """
261 #rep_address = Tuple(Str, Int)
262
264
263 # The PyZMQ Context to use for communication with the kernel.
265 # The PyZMQ Context to use for communication with the kernel.
264 context = Instance(zmq.Context, ())
266 context = Instance(zmq.Context, ())
@@ -266,38 +268,39 b' class KernelManager(HasTraits):'
266 # The Session to use for communication with the kernel.
268 # The Session to use for communication with the kernel.
267 session = Instance(Session, ())
269 session = Instance(Session, ())
268
270
271 # The channels objects used for communication with the kernel.
272 # FIXME: Add '_traitname_default' instantiation method to Traitlets.
273 #sub_channel = Instance(SubSocketChannel)
274 #xreq_channel = Instance(XReqSocketChannel)
275 #rep_channel = Instance(RepSocketChannel)
276
269 # The classes to use for the various channels.
277 # The classes to use for the various channels.
270 sub_channel_class = Type(SubSocketChannel)
278 sub_channel_class = Type(SubSocketChannel)
271 xreq_channel_class = Type(XReqSocketChannel)
279 xreq_channel_class = Type(XReqSocketChannel)
272 rep_channel_class = Type(RepSocketChannel)
280 rep_channel_class = Type(RepSocketChannel)
273
281
282 # The addresses to use for the various channels. Should be tuples of form
283 # (ip_address, port).
284 #sub_address = DelegatesTo('sub_channel')
285 #xreq_address = DelegatesTo('xreq_channel')
286 #rep_address = DelegatesTo('rep_channel')
287
274 # Protected traits.
288 # Protected traits.
275 _sub_channel = Any
289 _sub_channel = Any
276 _xreq_channel = Any
290 _xreq_channel = Any
277 _rep_channel = Any
291 _rep_channel = Any
278
292
279 def __init__(self, xreq_address, sub_address, rep_address, **traits):
293 def __init__(self, **traits):
280 super(KernelManager, self).__init__()
294 super(KernelManager, self).__init__()
281
295
282 self.xreq_address = xreq_address
283 self.sub_address = sub_address
284 self.rep_address = rep_address
285
286 # FIXME: This should be the business of HasTraits. The convention is:
296 # FIXME: This should be the business of HasTraits. The convention is:
287 # HasTraits.__init__(self, **traits_to_be_initialized.)
297 # HasTraits.__init__(self, **traits_to_be_initialized.)
288 for trait in traits:
298 for trait in traits:
289 setattr(self, trait, traits[trait])
299 setattr(self, trait, traits[trait])
290
300
291 def start_kernel(self):
301 def start_kernel(self):
292 """Start a localhost kernel on ip and port.
302 """Start a localhost kernel. If ports have been specified, use
293
303 them. Otherwise, choose an open port at random.
294 The SUB channel is for the frontend to receive messages published by
295 the kernel.
296
297 The REQ channel is for the frontend to make requests of the kernel.
298
299 The REP channel is for the kernel to request stdin (raw_input) from
300 the frontend.
301 """
304 """
302
305
303 def kill_kernel(self):
306 def kill_kernel(self):
@@ -314,22 +317,43 b' class KernelManager(HasTraits):'
314 def sub_channel(self):
317 def sub_channel(self):
315 """Get the SUB socket channel object."""
318 """Get the SUB socket channel object."""
316 if self._sub_channel is None:
319 if self._sub_channel is None:
317 self._sub_channel = self.sub_channel_class(
320 self._sub_channel = self.sub_channel_class(self.context,
318 self.context, self.session, self.sub_address)
321 self.session)
319 return self._sub_channel
322 return self._sub_channel
320
323
321 @property
324 @property
322 def xreq_channel(self):
325 def xreq_channel(self):
323 """Get the REQ socket channel object to make requests of the kernel."""
326 """Get the REQ socket channel object to make requests of the kernel."""
324 if self._xreq_channel is None:
327 if self._xreq_channel is None:
325 self._xreq_channel = self.xreq_channel_class(
328 self._xreq_channel = self.xreq_channel_class(self.context,
326 self.context, self.session, self.xreq_address)
329 self.session)
327 return self._xreq_channel
330 return self._xreq_channel
328
331
329 @property
332 @property
330 def rep_channel(self):
333 def rep_channel(self):
331 """Get the REP socket channel object to handle stdin (raw_input)."""
334 """Get the REP socket channel object to handle stdin (raw_input)."""
332 if self._rep_channel is None:
335 if self._rep_channel is None:
333 self._rep_channel = self.rep_channel_class(
336 self._rep_channel = self.rep_channel_class(self.context,
334 self.context, self.session, self.rep_address)
337 self.session)
335 return self._rep_channel
338 return self._rep_channel
339
340 def get_sub_address(self):
341 return self.sub_channel.addr
342 def set_sub_address(self, addr):
343 self.sub_channel.addr = addr
344 sub_address = property(get_sub_address, set_sub_address,
345 doc="The address used by SUB socket channel.")
346
347 def get_xreq_address(self):
348 return self.xreq_channel.addr
349 def set_xreq_address(self, addr):
350 self.xreq_channel.addr = addr
351 xreq_address = property(get_xreq_address, set_xreq_address,
352 doc="The address used by XREQ socket channel.")
353
354 def get_rep_address(self):
355 return self.rep_channel.addr
356 def set_rep_address(self, addr):
357 self.rep_channel.addr = addr
358 rep_address = property(get_rep_address, set_rep_address,
359 doc="The address used by REP socket channel.")
General Comments 0
You need to be logged in to leave comments. Login now