From cf52d0635bee98a727e40c7500a48fba892ef699 2012-05-15 04:55:15
From: MinRK <benjaminrk@gmail.com>
Date: 2012-05-15 04:55:15
Subject: [PATCH] add topic to display publisher, and fix set_parent for apply_requests
---

diff --git a/IPython/parallel/engine/engine.py b/IPython/parallel/engine/engine.py
index b5f5062..136af66 100644
--- a/IPython/parallel/engine/engine.py
+++ b/IPython/parallel/engine/engine.py
@@ -204,6 +204,7 @@ class EngineFactory(RegistrationFactory):
             self.kernel = Kernel(config=self.config, int_id=self.id, ident=self.ident, session=self.session,
                     control_stream=control_stream, shell_streams=shell_streams, iopub_socket=iopub_socket,
                     loop=loop, user_ns=self.user_ns, log=self.log)
+            self.kernel.shell.display_pub.topic = cast_bytes('engine.%i.displaypub' % self.id)
             self.kernel.start()
 
 
diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py
index 4c193a0..f374b41 100755
--- a/IPython/zmq/ipkernel.py
+++ b/IPython/zmq/ipkernel.py
@@ -527,16 +527,18 @@ class Kernel(Configurable):
         except:
             self.log.error("Got bad msg: %s", parent, exc_info=True)
             return
+
+        # Set the parent message of the display hook and out streams.
+        self.shell.displayhook.set_parent(parent)
+        self.shell.display_pub.set_parent(parent)
+        sys.stdout.set_parent(parent)
+        sys.stderr.set_parent(parent)
+
         # pyin_msg = self.session.msg(u'pyin',{u'code':code}, parent=parent)
         # self.iopub_socket.send(pyin_msg)
         # self.session.send(self.iopub_socket, u'pyin', {u'code':code},parent=parent)
         sub = self._make_subheader()
         try:
-            # allow for not overriding displayhook
-            if hasattr(sys.displayhook, 'set_parent'):
-                sys.displayhook.set_parent(parent)
-                sys.stdout.set_parent(parent)
-                sys.stderr.set_parent(parent)
             working = self.shell.user_ns
 
             prefix = "_"+str(msg_id).replace("-","")+"_"
diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py
index 222a965..b8d420f 100644
--- a/IPython/zmq/zmqshell.py
+++ b/IPython/zmq/zmqshell.py
@@ -43,7 +43,7 @@ from IPython.utils import io
 from IPython.utils.jsonutil import json_clean
 from IPython.utils.path import get_py_filename
 from IPython.utils.process import arg_split
-from IPython.utils.traitlets import Instance, Type, Dict, CBool
+from IPython.utils.traitlets import Instance, Type, Dict, CBool, CBytes
 from IPython.utils.warn import warn, error
 from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary
 from IPython.zmq.session import extract_header
@@ -60,6 +60,7 @@ class ZMQDisplayPublisher(DisplayPublisher):
     session = Instance(Session)
     pub_socket = Instance('zmq.Socket')
     parent_header = Dict({})
+    topic = CBytes(b'displaypub')
 
     def set_parent(self, parent):
         """Set the parent for outbound messages."""
@@ -82,7 +83,7 @@ class ZMQDisplayPublisher(DisplayPublisher):
         content['metadata'] = metadata
         self.session.send(
             self.pub_socket, u'display_data', json_clean(content),
-            parent=self.parent_header
+            parent=self.parent_header, ident=self.topic,
         )
 
     def clear_output(self, stdout=True, stderr=True, other=True):
@@ -97,7 +98,7 @@ class ZMQDisplayPublisher(DisplayPublisher):
         
         self.session.send(
             self.pub_socket, u'clear_output', content,
-            parent=self.parent_header
+            parent=self.parent_header, ident=self.topic,
         )
 
 class ZMQInteractiveShell(InteractiveShell):