Async container loading

Hi,

Im new to streamlit. I’m trying to come up with Chatbot concept that allows you to save results from QA session and reload them back another time. I wanted to see if I can reload the QA session asynchronously. It seems Streamlit doesn’t support multi threading out of the box - at least up to the latest version I have downloaded ( 1.43.0). I tried looking up some information from different posts such as :

https://discuss.streamlit.io/t/load-multiple-st-containers-in-parallel/53607

However the proposed solution in reddit seemed a bit complex and it did not seem to work for me.

I wanted to test something from scratch and see what happen using basic threads library and of course it did not work and I was getting a warning around ScriptRunContext , this lead me to the following post:

https://github.com/streamlit/streamlit/issues/1326

After tweaking my code a bit I think I got it working but Im not sure if this is correct and if it will not cause any unexpected behaviors down the line. Here is the code

import threading
import streamlit as st
import time
from streamlit.runtime.scriptrunner_utils.script_run_context import add_script_run_ctx

st.title("Cats!")


def orange_cats(user,bot,sleep_time):
    with st.container(height=200):
        with st.spinner("Wait for it...", show_time=True):
            time.sleep(sleep_time)
            st.title(user)
            st.markdown(bot)

def herd_orange_cats(messages):
    ctr = 0
    threads = []
    for msg in messages:
        thread = threading.Thread(target=orange_cats, args=(msg[0],msg[1],msg[2],)) 
        t =  add_script_run_ctx(thread) 
        #orange_cats(msg[0],msg[1],msg[2]) 
        threads.append(thread)
        t.start()
        ctr+=1
    for thread in threads:
        thread.join()
    
messages = []
for i in range(3):
    user = "🐈🐈"*(i+1)
    bot = "🐾 🐾🐾 🐾"*(i+1)
    messages.append([user,bot,(i+1)*5])

herd_orange_cats(messages)     
st.button("Herd all")

Appreciate the feedback.
Thanks