# Send progress through web socket

**URL:** https://discourse.vtk.org/t/send-progress-through-web-socket/7345
**Category:** Support
**Created:** [December 6, 2021, 2:39pm UTC](https://discourse.vtk.org/t/send-progress-through-web-socket/7345 "2021-12-06T14:39:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![no2497](https://discourse.vtk.org/user_avatar/discourse.vtk.org/no2497/32/3226_2.png) [@no2497](https://discourse.vtk.org/u/no2497)
#### Post date: [December 6, 2021, 2:39pm UTC](https://discourse.vtk.org/t/send-progress-through-web-socket/7345/1 "2021-12-06T14:39:54Z")

</div>

Hi  
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

```auto
...
reader = vtk.vtkDICOMReader()
reader.SetFileNames(filenames)
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?

Thanks

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [December 6, 2021, 3:56pm UTC](https://discourse.vtk.org/t/send-progress-through-web-socket/7345/2 "2021-12-06T15:56:21Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![no2497](https://discourse.vtk.org/user_avatar/discourse.vtk.org/no2497/32/3226_2.png) [@no2497](https://discourse.vtk.org/u/no2497)
#### Post date: [December 6, 2021, 3:57pm UTC](https://discourse.vtk.org/t/send-progress-through-web-socket/7345/3 "2021-12-06T15:57:52Z")

</div>

the event\_loop from asyncio and [websockets](https://websockets.readthedocs.io/en/stable/)

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [December 6, 2021, 6:41pm UTC](https://discourse.vtk.org/t/send-progress-through-web-socket/7345/4 "2021-12-06T18:41:58Z")

</div>

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).
