Send progress through web socket

While reading my DICOM series I want to send a message to the server notifying the current progress of the reading. I found out that there is the .addObserver() method which allows to register some events of a vtkObject. The problem is: I need to send this through a python websocket which uses the async/await notation. This is a snippet of my code

reader = vtk.vtkDICOMReader()
progress_event = ProgressEvent(websocket)
reader.AddObserver("ProgressEvent", progress_event)

class ProgressEvent:

    def __init__(self, websocket):
        self.websocket = websocket

    def __call__(self, caller, event):
        self.websocket.send('{"type": "progress", "amount": %s}' % caller.GetProgress())

This code does not work since it does not implement the async/await syntax. Setting it make so that the code inside the call function is not called.
Do you have any suggestion? Maybe there is another way to get a costant progress to send in some other way?


What does your program use for its event loop? Are you using Qt and QApplication.exec()? Or maybe vtkRenderWindowInteractor.Start()? The methods for asynchronous messaging will usually depend on what event loop you use.

the event_loop from asyncio and websockets

Since VTK events are always synchronous, the __call__ for your ProgressEvent must also be synchronous (as you have already guessed).

So I don’t think __call__() should directly call send(). Instead, __call__() should post a call to an async method (you’ll have to define one), and that async method should call send(). Then, as long as the event loop is running, the progress messages should go through, but the __call__() might also have to update the event loop (otherwise, if the reader’s Update() is running in the same thread as the loop, the loop will be blocked until the Update() is complete).