Time.sleep() weirdness

While trying to understand and fix Interaction between joblib and streamlit.cache, I’ve spotted something that I don’t understand at all:

# I'm not even importing streamlit!
# import streamlit as st

import time

do_sleep = True

def work(x):
    print("processing input", x)
    if do_sleep:
        time.sleep(1)
    return x * x

for i in range(1, 4):
    print("starting task", i)
    result = work(i)
    print("result", result)

Standard output:

starting task 1
processing input 1

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://10.0.0.212:8501

starting task 1
processing input 1
result 1
starting task 2
processing input 2
result 1
starting task 2
processing input 2
result 4
starting task 3
processing input 3
result 4
starting task 3
processing input 3
result 9
starting task 1
processing input 1
result 9
result 1
starting task 2
processing input 2
result 4
starting task 3
processing input 3
result 9

I don’t use streamlit cache. Why does the app decide to restart?

Hello @danielvarga !

I’m having a hard time reproducing your issue :confused: on Streamlit 0.56.0 I get the correct output :

starting task 1
processing input 1

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://192.168.1.3:8501

result 1
starting task 2
processing input 2
result 4
starting task 3
processing input 3
result 9

Can you write about your setup ?

  • Streamlit version: (get it with $ streamlit version)
  • Python version: (get it with $ python --version)
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version:

Oh wait, actually I managed to reproduce it but not consistently, and I’m not sure we’re in the same case…when I let a Streamlit tab open on my firefox, open 2 new tabs; stop the Streamlit app and rerun it, if the server creates a new tab on my browser then there will be 2 running Streamlit clients running on the app and I get the concurrent logs…if I close the app, open new tabs and rerun the app, if it succeeds in opening a new Streamlit tab then I have 3 Streamlit tabs hitting the server, one slower than the other two because it’s the one Streamlit creates whereas the other 2 already exist and I get exactly your output !

Are you sure you are not rerunning your Streamlit app while some clients are still trying to connect to it ? That may explain your output. I honestly don’t have other clues for now ^^

1 Like

Ouch, sorry for wasting your time! Indeed, there were two localhost:8501 tabs open among many, and that’s all there is to it. Thanks a lot for figuring it out from afar, impressive! Now the title is misleading, time.sleep() is innocent, should I delete or rename the question?

Well I’m not sure you can edit the title, and I think your post/title may help other people because really the time.sleep does contribute to the mixing of logs and this Time sleeping mechanic is used in the Streamlit docs to simulate a long process, so other people may try the same and fall into your situation, and will find your post through the search glass by typing Time.sleep

so I say let’s keep it that way :wink:

1 Like