The example in advanced_outputs.ipynb sending data from the kernel to the client is almost exactly what I need. And that example references the Jupyter comms documentation. But am I right that currently, although I can open a new connection to the client from the kernel with data attached, I cannot yet send multiple messages over the same connection?
The workflow that I think I want is e.g., in javascript:
(async () => {
google.colab.kernel.comms.registerTarget('example_comms', (comm, message) => {
document.body.appendChild(document.createTextNode('comm opened.'))
comm.send('comm opened');
console.log(comm)
comm.on_msg(function(msg) {
var p = document.createElement("p")
var t = document.createTextNode(message.data.foo)
p.appendChild(t)
document.body.appendChild(p)
})
});
})()
and in python
channel = comm.Comm(target_name='example_comms')
def handle_message(msg):
print(f"python received message: {msg['content']['data']}")
channel.on_msg(handle_message)
for i in range(10):
channel.send(data={'foo': i})
But it seems that google.colab.kernel.comms.Comm does not export on_msg
, nor any other way that I can see to send multiple messages over the same channel? The ipykernel.comm.Comm
object has a send
method, but I don't think it can be used at present?
Here is a notebook with minimal modifications from the advanced_outputs example. Running it throws an error in the console that on_msg
is not defined.
I suspect (but admittedly have not yet measured the performance to confirm) that making a new connection for every message is overhead that I want to avoid. Any help is appreciated!
`Line # Hits Time Per Hit % Time Line Contents ` ============================================================== 84 501 895220.0 1786.9 100.0 comm.Comm(target_name="meshcat", data=command.lower()) – Russ Tedrake Aug 06 '20 at 10:25