##// END OF EJS Templates
* Implemented KernelManager's 'signal_kernel' method....
epatters -
Show More
@@ -338,16 +338,12 b' class KernelManager(HasTraits):'
338 338 _xreq_channel = Any
339 339 _rep_channel = Any
340 340
341 def __init__(self, **traits):
342 super(KernelManager, self).__init__()
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])
341 #--------------------------------------------------------------------------
342 # Channel management methods:
343 #--------------------------------------------------------------------------
348 344
349 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 347 a RuntimeError.
352 348 """
353 349 if self.is_listening:
@@ -358,17 +354,33 b' class KernelManager(HasTraits):'
358 354 self.xreq_channel.start()
359 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 366 def stop_listening(self):
362 """Stop listening. If not listening, does nothing. """
367 """Stops listening. If not listening, does nothing. """
363 368 if self.is_listening:
364 369 self.is_listening = False
365 370 self.sub_channel.stop()
366 371 self.xreq_channel.stop()
367 372 self.rep_channel.stop()
368 373
374 #--------------------------------------------------------------------------
375 # Kernel process management methods:
376 #--------------------------------------------------------------------------
377
369 378 def start_kernel(self):
370 """Start a localhost kernel. If ports have been specified via the
371 address attributes, use them. Otherwise, choose open ports at random.
379 """Starts a kernel process and configures the manager to use it.
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 385 xreq, sub = self.xreq_address, self.sub_address
374 386 if xreq[0] != LOCALHOST or sub[0] != LOCALHOST:
@@ -376,30 +388,49 b' class KernelManager(HasTraits):'
376 388 "Make sure that the '*_address' attributes are "
377 389 "configured properly.")
378 390
379 self._kernel, xrep, pub = launch_kernel(xrep_port=xreq[1],
380 pub_port=sub[1])
391 kernel, xrep, pub = launch_kernel(xrep_port=xreq[1], pub_port=sub[1])
392 self.set_kernel(kernel)
381 393 self.xreq_address = (LOCALHOST, xrep)
382 394 self.sub_address = (LOCALHOST, pub)
383 395
384 def kill_kernel(self):
385 """Kill the running kernel, if there is one.
396 def set_kernel(self, kernel):
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 422 if self._kernel:
388 423 self._kernel.kill()
389 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 425 else:
398 return False
426 raise RuntimeError("Cannot kill kernel. No kernel is running!")
399 427
400 428 def signal_kernel(self, signum):
401 """Send signum to the kernel."""
402 # TODO: signal the kernel.
429 """ Sends a signal to 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 436 # Channels used for communication with the kernel:
@@ -430,7 +461,7 b' class KernelManager(HasTraits):'
430 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 467 def get_sub_address(self):
General Comments 0
You need to be logged in to leave comments. Login now