Why does front and back end connection gets suspended when time.sleep() is running?

Hi guys!

Is there some alternative to time.sleep() to prevent suspending connection between front and back end? Is there a way to prevent changes in the UI while pausing?

To work around this issue, you should avoid using blocking operations like time.sleep within the main thread of your Streamlit app. Instead, consider using Streamlit’s built-in mechanisms for handling delays and updates. For example, you can use st.spinner to display a spinner while a long-running process is taking place, or use widgets like buttons to trigger actions without blocking the main thread.Here’s an example of how you can use st.spinner:
import streamlit as st
import time

with st.spinner(“Processing…”):
# Perform your time-consuming task here, but don’t use time.sleep

# Simulate a task that takes time

Once the task is done, the spinner will automatically disappear.

st.success(“Task completed!”)This approach keeps your Streamlit app responsive and ensures the connection between the front-end and back-end remains active.

Hi @sai_krishna1, thank you for the answer!

I’m creating a real-time monitoring system for a graph data acquisition module. For the most part, I’m using MUI. Every time I’m using the st.spinner, the entire MUI dashboard goes away, while the spinner is only visible. When the processes within st.spinner() finish, the dashboard is visible again. I would like to avoid that.

The reason I’m using time delay is to avoid reaching quotas when requesting data from the server. However, reaching the quotas is inevitable, so a status code 429 is returned or some other status code, letting me know that the request was unsuccessful. In these cases, the logic is to enter a while loop, make a request every t seconds, until the request is successful.

I’m looking for a way to implement this logic with MUI, without sacrificing the UI experience. The desired outcome would be to have a countdown, while the entire app is still responsive. However, I’m not sure it’s possible with streamlit.

The best solution I’ve found is this implementation but I’m facing the same issue, as with st.spinner(); the entire dashboard goes away until the countdown is finished.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.