如何在IPython中向ThreadedKernelClient添加回调?

时间:2015-10-30 19:53:31

标签: ipython jupyter

目前,我正在接受ThreadedKernelClient

kernel_manager = KernelManager(client_class='IPython.kernel.threaded.ThreadedKernelClient')
kernel_manager.start_kernel()
kernel_client = kernel_manager.client()

但是,我不确定如何为传入的消息注册回调(在我运行execute之后)。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

我所做的是子类ThreadedZMQSocketChannelThreadedKernelClient,并覆盖call_handlers,例如:

class TestChannel(ThreadedZMQSocketChannel):
    def call_handlers(self, msg):
        # do something


class TestClient(ThreadedKernelClient):
    iopub_channel_class = Type(TestChannel)
    shell_channel_class = Type(TestChannel)
    stdin_channel_class = Type(TestChannel)

您的经理看起来像:

kernel_manager = KernelManager(kernel_name='python', client_class='__main__.TestClient')

start_channels后,您的call_handlers方法将使用execute等方法的结果进行调用。