Correct pattern for async calls #236
-
Hi, I have a Trame project, where I am using SimpleITK to load some medical images, and then feed them into a processing pipeline. I have a VProgressLinear bar that I want to update as the images load. I am struggling to find the correct way to call the SimpleITK function, which loads the image, and updates the progress using an sitk.Command. My code is something these lines:
and
My load_dicom_series function looks something like :
In my progress_cmd, I update the state using
As the image loads, the progress variable increments as expected, and I can see the state variable incrementing as well. The issue is that the state doesn't get sent to the front-end, until after the Obviously the SimpleITK filter is a non-awaitable function, so I expect it needs to be wrapped somehow. So I understand that there is something incorrect in how I am trying to kicj off task in a different thread, but I've tried a number of approaches like wrapping the
or
and some combinations of the above. None seem to work. I THINK the idea or solution is to have the back-end task run on a thread that is not the main thread, and update the state object, and to have the main thread update the front-end state. Unfortunately, the Python async-await implementation leaves a lot to be desired in terms of documentation and interface, so I am somewhat at a loss. The TLDR of my question is "What is the correct pattern to have a button on the web front-end, trigger a task on the back-end, which will update the state, so that the Trame server will send that state back to the Front-End?" |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
When you have async update of the state, you need to be explicit when you want that state to be updated. The easier approach is to do state change in the state context like below: async do_something():
while True:
await asyncio.sleep(0.5)
with state:
state.progress += 1 Otherwise you can call state.flush() |
Beta Was this translation helpful? Give feedback.
When you have async update of the state, you need to be explicit when you want that state to be updated.
The easier approach is to do state change in the state context like below:
Otherwise you can call state.flush()