Skip to content

KernelClient: factor out generic async client from Qt client - #7196

Merged
takluyver merged 2 commits into
ipython:masterfrom
bfredl:asyncclient
Dec 18, 2014
Merged

takluyver merged 2 commits into
ipython:masterfrom
bfredl:asyncclient

Conversation

@bfredl

@bfredl bfredl commented Dec 12, 2014

Copy link
Copy Markdown
Contributor

before #7108 it was simple for a third-party front-end to recieve message notifications asynchronously from (non-blocking) KernelClient by overriding, or even monkeypatching the call_handlers method on the channels like this

self.kernel_client.shell_channel.call_handlers = self.on_shell_msg
self.kernel_client.iopub_channel.call_handlers = self.on_iopub_msg
...

This is not as simple anymore; now qt.client implements the requied logic (a single ioloop thread) but it is tied to the Qt signal/slot mechanism. This PR splits out a generic async kernel client from QtKernelClient, that essentially preserves the behaviour of the old KernelClient (execpt that is uses less threads, thanks to #7108).

The only (intended) "semantical" change is that _check_kernel_info_client is moved outside call_handlers (so that subclasses don't need to care about this), but I don't know the exact purpose this have, so I can't be entirely sure this is handled correctly.

I'm not sure AsyncKernelClient is the best name tho, perhaps ThreadKernelClient is more explicit?

@ccordoba12

Copy link
Copy Markdown
Member

Could you point to a code example that used the old behavior?

@bfredl

bfredl commented Dec 12, 2014

Copy link
Copy Markdown
Contributor Author

Sure, here is a (work in progress) Neovim frontend ( even more WIP ipy3-dev port here ). It is based on/similar to ivanov/vim-ipython , expect that communication between Neovim and the kernel is asynchronous. The code is a bit messy, but here is the part connecting to the client:

class IPythonVimApp(BaseIPythonApplication, IPythonConsoleApp):
    kernel_client_class = KernelClient
    if ipy3:
        kernel_client_class = AsyncKernelClient
    kernel_manager_class = RedirectingKernelManager
    def init_kernel_client(self):
        self.kernel_client = self.kernel_manager.client()
        self.kernel_client.shell_channel.call_handlers = self.target.on_shell_msg
        self.kernel_client.iopub_channel.call_handlers = self.target.on_iopub_msg
        self.kernel_client.stdin_channel.call_handlers = self.target.on_stdin_msg
        self.kernel_client.hb_channel.call_handlers = self.target.on_hb_msg
        self.kernel_client.start_channels()

    def initialize(self, target, argv):
        self.target = target
        super(IPythonVimApp, self).initialize(argv)
        IPythonConsoleApp.initialize(self, argv)

Comment thread IPython/kernel/async/client.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that _check_kernel_info_reply() is now getting called in the ioloop thread, whereas previously it was called in the main application thread. I think this is safe, but at a minimum, we should put comments in _check_kernel_info_reply and _handle_kernel_info_reply mentioning this fact, so we don't do something later that assumes they're in the main thread.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realize when i did this, than Qt signals sent in a helper thread are handled in the main thread. I will look a bit more into the client code and try see if this is thread-safe.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be fine - it's handing over the already-parsed message to be handled. If anything in either handler were to modify the dict in-place, it would not be threadsafe, but message dicts should not be modified, only read from, so we should be okay.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And even then, it's only possible to have an issue when there are two listeners on the same message - one on the QtSignal and one on the inner loop, one of which is modifying the message content, which it shouldn't do.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the thread safety of the changes it makes to session to set up the protocol adaptation. But on thinking some more, I guess that session is mostly/only used from the ioloop thread once it's been set up, so it should be OK.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes session.adapt_version is both set and used in the ioloop thread, so this should be safe.

@takluyver

Copy link
Copy Markdown
Member

Thanks, I like this approach - it reintroduces the important bit of flexibility from the old design, but with very minimal extra complexity added to the current state.

I do want to think a bit more about the name. There's more than one way to build an async kernel client, and this is rather the quick and dirty way. The more elegant approach would be to integrate the ZMQ sockets into the application's own event loop. Maybe ThreadedKernelClient, but that still seems a bit misleading, because ZMQ runs its own C-level threads to send and receive messages*, so any ZMQ client is threaded.

(*this makes our ioloop thread completely redundant: ZMQ is doing the same thing internally, reading from sockets in a thread and making callbacks when a message is ready.)

@takluyver

Copy link
Copy Markdown
Member

The only (intended) "semantical" change is that _check_kernel_info_client is moved outside call_handlers (so that subclasses don't need to care about this), but I don't know the exact purpose this have

The special handling of kernel_info_reply is so that the client can work with kernels that don't speak the newest version of the protocol. It makes a kernel info request when the kernel starts, and the reply tells it what version of the protocol the kernel is using. The client can then translate between the protocol the kernel uses and the protocol the client expects.

@bfredl

bfredl commented Dec 17, 2014

Copy link
Copy Markdown
Contributor Author

Thank you for your comments. I would say ThreadedKernelClient is quite good, because this solution implies that application-level code will run in a separate thread (and must handle thread-safety). AFAIK no callbacks is done inside the internal worker thread of ZMQ into user code (but correct me if I'm wrong), so in the "elegant" solution there could potentially be only one thread running application code.

@bfredl

bfredl commented Dec 18, 2014

Copy link
Copy Markdown
Contributor Author

Updated: renamed to Threaded* and added comments about thread-safety of kernel-info reply.

Comment thread IPython/kernel/threaded/__init__.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than making this a new package with only one code file, let's just have a threaded.py file under kernel. We can transparently turn it into a package later if we need more code in there.

@bfredl

bfredl commented Dec 18, 2014

Copy link
Copy Markdown
Contributor Author

Ok, done

@takluyver

Copy link
Copy Markdown
Member

Thanks. Once Travis has checked it again, I'll hit the merge button.

@bfredl

bfredl commented Dec 18, 2014

Copy link
Copy Markdown
Contributor Author

There seems to be a name conflict on 2.7 about zmq, after moving the module. Should I from __future__ import absolute_import ?

@takluyver

Copy link
Copy Markdown
Member

Good catch, and I completely agree with your fix.

@bfredl

bfredl commented Dec 18, 2014

Copy link
Copy Markdown
Contributor Author

Thanks, now it passes.

@takluyver

Copy link
Copy Markdown
Member

Great, thank-you!

takluyver added a commit that referenced this pull request Dec 18, 2014
KernelClient: factor out generic async client from Qt client
@takluyver
takluyver merged commit 0b6cb32 into ipython:master Dec 18, 2014
@minrk minrk modified the milestone: 3.0 Jan 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants