Multithreading

Hi Team,

I am new to Python and streamlit and working on a project. The model training part of my project takes a while during which the entire screen freezes and the user has to wait for some time before the results get displayed. The users cannot navigate to a different page while the model is being trainiend as the screen has frozen. I want to enable multi threading so that the training takes place on the secondary thread and the main thread continues. I have used the below piece of code:

The issue that I’m facing is that even though I have assigned a context to the thread, I’m getting below error on the terminal. What possibly could be going wrong?

I also want to display the training results (best model, hyperparameter values, etc) from the new_thread thread on the streamlit app, but I’m unable to do so. They’re getting displayed on the terminal but not on the app. Is this expected?

Once, the new thread (secondary thread)completes, I also want to device some mechanism that tells the main thread that the secondary thread has completed. is_alive() method checks whether a thread is alive and returns a boolean value based on the thread status but if I use this, I need to constantly run this command to check the status of my new thread. Can you please advise here as well?

Thanks a lot!

Hi @randyzwitch, I know there are several related discussions here, I tried them but no luck. Can you please look into this and advise?

t = threading.Thread(...............)
streamlit.scriptrunner.add_script_run_ctx(t)  # <---- this one
t.start()

Look at the Google for the more description. I do not understand the behavior exactly as I am dealing with this right now, but you’l find more precise information, I am sure. Note this is valid for StreamLit >= 1.8