'sleep' causes function to reload despite of cache

I’m trying to send a request to an external API, wait some time and get back the response. Here’s a minimal example of my code (I replaced the requests with just logs):

import streamlit as st
from time import sleep

@st.cache
def run():
    print('Send some request and wait some time')
    sleep(5)
    print('Request the response')

if __name__ == '__main__':
    run()

When I run it (streamlit run app.py), the output is:

Send some request and wait some time
Send some request and wait some time
Request the response
Request the response

In other words, it is executing the function twice, which shouldn’t happen. I notice the problem goes away when I remove the sleep (but of course I need it for my app to run properly).

Hi @rsilva,

Can you try updating the version of Streamlit you’re using? Also what version do you have currently? I just tried your code snippet on my machine here and it seems to be working fine, only running one time and printing to the terminal (I attached a screenshot for you). I’ve got Streamlit 1.2.0 installed in this virtual environment.

Happy Streamlit-ing!
Marisa

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