Hey
I want to run a code concurrently with streamlit, iām trying to use threads, and Iām getting this error:
2022-08-19 08:09:33.852 Thread 'Thread-11': missing ScriptRunContext
Code:
from pathlib import Path
from threading import Thread
def target():
st.text(c)
if __name__ == '__main__':
if not st.session_state.get("thread"):
st.session_state["thread"] = True
t = Thread(target=target)
t.start()
Can you please help ? What is the best solution to use concurrency with streamlit?
Hi @royassis , welcome to the Streamlit community!
Hereās a GitHub issue where the solution is discussed in the comments:
opened 08:15PM - 08 Apr 20 UTC
enhancement
If you create a thread from your Streamlit script, and call streamlit functions ⦠from within that thread, you need to attach a `ReportContext` to it or else your `st.foo()` calls won't do anything, and you'll get a warning message that looks something like this:
āThread āThread-7ā: missing ReportContextā
We should improve the error message to tell the user how to fix this! (We may just want an FAQ entry that we can link to from the error message.)
(The fix is to use `add_report_ctx` on the thread immediately after it's created:)
```python
from streamlit.ReportThread import add_report_ctx
thread = threading.Thread(target=...)
add_report_ctx(thread)
thread.start()
```
Related discussion: https://discuss.streamlit.io/t/how-to-run-a-subprocess-programs-using-thread-inside-streamlit/2440
(2022 edit: please see my comments [here](https://github.com/streamlit/streamlit/issues/1326#issuecomment-1024399877) - this is an internal Streamlit API and has some major caveats. In particular, please *do not* call `st.foo` commands from other threads - at least in production code that needs to be stable.)
Thanks @snehankekre .
I added an example of how to import add_script_run_ctx
in streamlit==1.12.0
asehmi
August 20, 2022, 10:33pm
#4
@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
asehmi
October 11, 2022, 9:28am
#5
Hi @snehankekre - do you know how to get this script to work, i.e. report message in Streamlit?