Information found at the Jupyter documentation pages left me a little confused. My first impression was that communication between frontend and kernel is performed through 3 ZeroMQ connections: Shell (Router-Dealer), IOPub (Publisher-Subscriber) and Stdin (Router-Dealer). Further reading of the docs mentioned other 2 connection channels, Control and Heartbeat, but there was less info about their roles and how to implement them.
Obviously some informations are outdated, as IPython is the predecessor of Jupyter, but the general architecture remained the same:
According to the article, the ZeroMQ socket types for each of those connections are the following:
Heartbeat: Reply (Request on frontends)
Shell, Control, Stdin: Router (Dealer on frontends)
IOPub: Publisher (Subscriber on frontends)
Now that we got the general idea, let’s move to the implementation.
It’s pretty trivial to write the code to just instantiate the ZeroMQ connections, but there was some details I didn’t know.
As mentioned in the previous post, another IPython project, the simple_kernel.py by Doug Blank, gave me some insights about how to make things work: creating a separate thread for the Heartbeat socket guarantees that it answers immediately to the client requests (some kind of “Are you there yet ?” messages), keeping it aware that the backend is still running.
Thanks to the C++11 standard threads, thread creation and management in C++ can be done in a portable manner.
The kernel code that I have for now is shown below (and you can also see it on my GitHub repository). Obviously it is not functional, as this only create the required sockets and runs the Heartbeat thread:
Having compiled the code, we setup the configuration files according to the Jupyter docs to call it from the console (Jupyter client) with:
The result we get is the following:
As you can see, not even input is requested, and we can’t send an exit command, as I didn’t implement these things yet. But the kernel was correctly found and started.
You can see from the docs and the console output, there are .json configuration files being read. This involves an important aspect of Jupyter about which I’ll talk later.
That’s it for now. Thanks for reading and until next time !