Concurrency with streamlit

@snehankekre @royassis
I was unable to make this code below work. Any ideas how to fix it?

The loop in the thread target works because I see the console print outs, but only the first iteration is printed in Streamlit.

import time
import streamlit as st
import threading
try:
    # Streamlit >= 1.12.0
    from streamlit.runtime.scriptrunner import add_script_run_ctx
    from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx
except:
    # Streamlit <= 1.11.0
    from streamlit.scriptrunner import add_script_run_ctx
    from streamlit.scriptrunner.script_run_context import get_script_run_ctx

def st_write_log(message):
    st.write(message)
    print(message)

def thread_target(iterations, seconds):
    for i in range(iterations):
        st_write_log(f"thread_target ({i})")
        time.sleep(seconds)
    st_write_log('=== END (Refresh or CTRL-C) ===')

t = threading.Thread(target=thread_target, args=(5,1))
ctx = get_script_run_ctx()
print('=== CTX ===\n', ctx)
add_script_run_ctx(t)
t.start()

I haven’t been able to install Streamlit 1.12.0 successfully yet, so haven’t tried it in that version yet.

Thanks,
Arvindra