From 452f07b9e67b78e1b11ba82d651d89bd4d55dc03 2010-09-17 04:19:29 From: Brian Granger Date: 2010-09-17 04:19:29 Subject: [PATCH] Draft of context closing. I have added atexit.register(context.close) to both the kernel and the frontend. This required pyzmq master and zeromq 2.0.8 or 2.0.9. This code is commented out for now, but it is easy to enable and test. --- diff --git a/IPython/zmq/entry_point.py b/IPython/zmq/entry_point.py index 31ca70b..d049448 100644 --- a/IPython/zmq/entry_point.py +++ b/IPython/zmq/entry_point.py @@ -3,6 +3,7 @@ launchers. """ # Standard library imports. +import atexit import os import socket from subprocess import Popen, PIPE @@ -84,6 +85,8 @@ def make_kernel(namespace, kernel_factory, # Create a context, a session, and the kernel sockets. io.raw_print("Starting the kernel at pid:", os.getpid()) context = zmq.Context() + # Uncomment this to try closing the context. + # atexit.register(context.close) session = Session(username=u'kernel') reply_socket = context.socket(zmq.XREP) diff --git a/IPython/zmq/kernelmanager.py b/IPython/zmq/kernelmanager.py index bfda0d4..b5500fc 100644 --- a/IPython/zmq/kernelmanager.py +++ b/IPython/zmq/kernelmanager.py @@ -16,6 +16,7 @@ TODO #----------------------------------------------------------------------------- # Standard library imports. +import atexit from Queue import Queue, Empty from subprocess import Popen import signal @@ -654,7 +655,7 @@ class KernelManager(HasTraits): sub_channel_class = Type(SubSocketChannel) rep_channel_class = Type(RepSocketChannel) hb_channel_class = Type(HBSocketChannel) - + # Protected traits. _launch_args = Any _xreq_channel = Any @@ -662,6 +663,10 @@ class KernelManager(HasTraits): _rep_channel = Any _hb_channel = Any + def __init__(self, **kwargs): + super(KernelManager, self).__init__(**kwargs) + atexit.register(self.context.close) + #-------------------------------------------------------------------------- # Channel management methods: #--------------------------------------------------------------------------