St.fragment run_every slowing down when streamlit tab is not active

When running the following code locally

import streamlit as st
import time

if 'count' not in st.session_state:
    st.session_state['count'] = 0
if 'time' not in st.session_state:
    st.session_state['time'] = time.time()


@st.fragment(run_every=5)
def main():
    print('\n')
    st.session_state['count'] += 1
    print('Count', st.session_state['count'])
    print('Time difference', st.session_state['time'] - time.time())
    st.session_state['time'] = time.time()


main()

If the streamlit tab is not the active tab, after around 14-15 iterations, the time difference increases to >> 5 s. This happens with both Edge and Chrome.

Example log

Count 1
Time difference -0.0022592544555664062


Count 2
Time difference -6.134297609329224


Count 3
Time difference -5.005406856536865


Count 4
Time difference -4.993649005889893


Count 5
Time difference -4.99873685836792


Count 6
Time difference -4.999541282653809


Count 7
Time difference -5.0076634883880615


Count 8
Time difference -4.995625019073486


Count 9
Time difference -5.002286195755005


Count 10
Time difference -4.991834402084351


Count 11
Time difference -5.015696287155151


Count 12
Time difference -4.989091873168945


Count 13
Time difference -5.004159927368164


Count 14
Time difference -55.98618197441101

Streamlit version: 1.39
Python version: 3.12
Browser: Chrome, Edge

1 Like

I tried that code locally (Arch Linux, python 3.12, streamlit 1.41).

  • In Firefox, shitching tabs made no difference.
  • In Gnome Web, switching tabs increased the differences by one second exactly. Switching back to the streamlit tab restored the five seconds differences.

So I couldn’t reproduce your results but I found some weird behavior where the browser seems to be involved in some way.

1 Like