ipython / ipython/ipykernel

[Question] Asynchronous Comm.on_msg callback

Aperta
#542 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
734
Fork
412
Merge medio
1g 5h
PR unite (30g)
8

Descrizione

I'm registering a callback function that forward every messages associated to given registered target to a ZMQ socket (which is a remote server). It works great when the main thread is idle, but if the main thread is busy for some reason, the callback is not call until the main thread finishes its processing. It is a big issue in by case because in practice I'm busy waiting for the server to acknowledge it has received the data...

Here is how I'm registering the callback:
```
class ZMQIPythonBridge(threading.Thread):
def __init__(self, comm_url):
threading.Thread.__init__(self)
self.zmq_url = zmq_url

def comm_register(self, comm, msg):
@comm.on_msg
def _on_msg(msg):
data = msg['content']['data']
self.comm_zmq.send(f"data:{comm.comm_id}:{data}".encode())

self.comm_zmq.send(f"open:{comm.comm_id}".encode())

def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
ioloop = tornado.ioloop.IOLoop()
ioloop.make_current()
context = zmq.Context()
self.zmq_socket = context.socket(zmq.XREQ)
self.zmq_socket.connect(self.zmq_url)
get_ipython().kernel.comm_manager.register_target(
'my_channel', self.comm_register)
ioloop.start()

comm_bridge = ZMQIPythonBridge(comm_url)
comm_bridge.start()
```

Is there a way to still be enable to trigger `_on_msg` while the main thread is busy wait for the ZMQ socket to acknowledge ? I tried to use a different thread, but as expected in it not changing anything (since the callback mechanism is still handle by the main thread...)

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start by tracing the `comm.on_msg` callback, `comm_manager.register_target`, and the Tornado `IOLoop` setup shown in the issue, then reproduce the behavior while the main thread busy-waits on the ZMQ socket. Done means determining whether callbacks can run during that wait and documenting the supported approach or limitation.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
jupyter, python
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.