Trying to create an app through NiceGUI.I have a button on which an API is called .But API takes time to respond and app loses connection. Any fixes?

1.5k views Asked by At

I'm creating an app using NiceGUI (Python). I have a button which upon getting clicked makes an API call. However that API takes a lot of time to do the work in the backend and the app loses connection and shows the error as:

"Trying to connect. Lost connection".

Is there any fix for this. Can I prevent the app to lose connection and stay active while the API does its work ?

Trying to make an API call which takes a lot of time to do the work in the backend and in the mean time app loses connection. I want the app to stay active while API does the work. I cannot tweak the time of API to do the work because it's a pre built API. How can I make the app stay active ?

1

There are 1 answers

3
Falko On BEST ANSWER

This issue has been discussed here:

You should offload all heavy work with async/await. NiceGUI (and the underlying FastAPI) are async frameworks. That means, no io-bound or cpu-bound tasks should be directly executed on the main thread but rather be "awaited".

You should have a look into asyncio's run_in_executor which allows you to await long-running tasks while the UI stays responsive.