1

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 ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kshitiz_sc
  • 45
  • 2

1 Answers1

1

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.

Falko
  • 17,076
  • 13
  • 60
  • 105