##// END OF EJS Templates
* Implemented KernelManager's 'signal_kernel' method....
epatters -
Show More
@@ -338,16 +338,12 b' class KernelManager(HasTraits):'
338 _xreq_channel = Any
338 _xreq_channel = Any
339 _rep_channel = Any
339 _rep_channel = Any
340
340
341 def __init__(self, **traits):
341 #--------------------------------------------------------------------------
342 super(KernelManager, self).__init__()
342 # Channel management methods:
343
343 #--------------------------------------------------------------------------
344 # FIXME: This should be the business of HasTraits. The convention is:
345 # HasTraits.__init__(self, **traits_to_be_initialized.)
346 for trait in traits:
347 setattr(self, trait, traits[trait])
348
344
349 def start_listening(self):
345 def start_listening(self):
350 """Start listening on the specified ports. If already listening, raises
346 """Starts listening on the specified ports. If already listening, raises
351 a RuntimeError.
347 a RuntimeError.
352 """
348 """
353 if self.is_listening:
349 if self.is_listening:
@@ -358,17 +354,33 b' class KernelManager(HasTraits):'
358 self.xreq_channel.start()
354 self.xreq_channel.start()
359 self.rep_channel.start()
355 self.rep_channel.start()
360
356
357 @property
358 def is_alive(self):
359 """ Returns whether the kernel is alive. """
360 if self.is_listening:
361 # TODO: check if alive.
362 return True
363 else:
364 return False
365
361 def stop_listening(self):
366 def stop_listening(self):
362 """Stop listening. If not listening, does nothing. """
367 """Stops listening. If not listening, does nothing. """
363 if self.is_listening:
368 if self.is_listening:
364 self.is_listening = False
369 self.is_listening = False
365 self.sub_channel.stop()
370 self.sub_channel.stop()
366 self.xreq_channel.stop()
371 self.xreq_channel.stop()
367 self.rep_channel.stop()
372 self.rep_channel.stop()
368
373
374 #--------------------------------------------------------------------------
375 # Kernel process management methods:
376 #--------------------------------------------------------------------------
377
369 def start_kernel(self):
378 def start_kernel(self):
370 """Start a localhost kernel. If ports have been specified via the
379 """Starts a kernel process and configures the manager to use it.
371 address attributes, use them. Otherwise, choose open ports at random.
380
381 If ports have been specified via the address attributes, they are used.
382 Otherwise, open ports are chosen by the OS and the channel port
383 attributes are configured as appropriate.
372 """
384 """
373 xreq, sub = self.xreq_address, self.sub_address
385 xreq, sub = self.xreq_address, self.sub_address
374 if xreq[0] != LOCALHOST or sub[0] != LOCALHOST:
386 if xreq[0] != LOCALHOST or sub[0] != LOCALHOST:
@@ -376,30 +388,49 b' class KernelManager(HasTraits):'
376 "Make sure that the '*_address' attributes are "
388 "Make sure that the '*_address' attributes are "
377 "configured properly.")
389 "configured properly.")
378
390
379 self._kernel, xrep, pub = launch_kernel(xrep_port=xreq[1],
391 kernel, xrep, pub = launch_kernel(xrep_port=xreq[1], pub_port=sub[1])
380 pub_port=sub[1])
392 self.set_kernel(kernel)
381 self.xreq_address = (LOCALHOST, xrep)
393 self.xreq_address = (LOCALHOST, xrep)
382 self.sub_address = (LOCALHOST, pub)
394 self.sub_address = (LOCALHOST, pub)
383
395
384 def kill_kernel(self):
396 def set_kernel(self, kernel):
385 """Kill the running kernel, if there is one.
397 """Sets the kernel manager's kernel to an existing kernel process.
398
399 It is *not* necessary to a set a kernel to communicate with it via the
400 channels, and those objects must be configured separately. It
401 *is* necessary to set a kernel if you want to use the manager (or
402 frontends that use the manager) to signal and/or kill the kernel.
403
404 Parameters:
405 -----------
406 kernel : Popen
407 An existing kernel process.
408 """
409 self._kernel = kernel
410
411 @property
412 def has_kernel(self):
413 """Returns whether a kernel process has been specified for the kernel
414 manager.
415
416 A kernel process can be set via 'start_kernel' or 'set_kernel'.
386 """
417 """
418 return self._kernel is not None
419
420 def kill_kernel(self):
421 """ Kill the running kernel. """
387 if self._kernel:
422 if self._kernel:
388 self._kernel.kill()
423 self._kernel.kill()
389 self._kernel = None
424 self._kernel = None
390
391 @property
392 def is_alive(self):
393 """ Returns whether the kernel is alive. """
394 if self.is_listening:
395 # TODO: check if alive.
396 return True
397 else:
425 else:
398 return False
426 raise RuntimeError("Cannot kill kernel. No kernel is running!")
399
427
400 def signal_kernel(self, signum):
428 def signal_kernel(self, signum):
401 """Send signum to the kernel."""
429 """ Sends a signal to the kernel. """
402 # TODO: signal the kernel.
430 if self._kernel:
431 self._kernel.send_signal(signum)
432 else:
433 raise RuntimeError("Cannot signal kernel. No kernel is running!")
403
434
404 #--------------------------------------------------------------------------
435 #--------------------------------------------------------------------------
405 # Channels used for communication with the kernel:
436 # Channels used for communication with the kernel:
@@ -430,7 +461,7 b' class KernelManager(HasTraits):'
430 return self._rep_channel
461 return self._rep_channel
431
462
432 #--------------------------------------------------------------------------
463 #--------------------------------------------------------------------------
433 # Channel address attributes:
464 # Delegates for the Channel address attributes:
434 #--------------------------------------------------------------------------
465 #--------------------------------------------------------------------------
435
466
436 def get_sub_address(self):
467 def get_sub_address(self):
General Comments 0
You need to be logged in to leave comments. Login now