St.memization_memo ttl question

Hi all,

Just a simple question - the ttl option on the st.memoization_memo decorator - after the number of seconds you define in that, does the cache automatically refresh? I have a function which calls a request which updates on its home website after a day, and I wish to have a function that automatically calls a new version of that request after X seconds, and so I’ve listed it in the ttl option.

Just wondering if that was how it worked, or whether my understanding is incorrect?

Thank you for your great work

Sang

The ttl means that if you call the function after that many seconds has passed, then the function will be re-run. Otherwise, it will simply return the previously-returned value. It won’t automatically rerun the function after that many seconds have passed. If you have something that needs to be re-run continually, here’s one way you can accomplish that:

from datetime import datetime
from time import sleep

import streamlit as st


@st.experimental_memo(ttl=3)
def get_time():
    return datetime.now()

time_goes_here = st.empty()

while True:
    # Should update every 3 seconds
    time_goes_here.write(f"# Current time {get_time()}")
    sleep(1)
2 Likes

Hi Blackary,

Thank you for the reply. I’ll give this method a try.

1 Like

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