streamlit / docs

Source code for the Streamlit Python library documentation
https://docs.streamlit.io
Apache License 2.0
110 stars 457 forks source link

Add documentation about how to use Streamlit with multiple threads. #87

Open tvst opened 5 years ago

tvst commented 5 years ago

TLDR: you either need to use the ReportThread class for your thread, or call add_report_ctx on your thread after creation

mansidak commented 1 year ago

@tvst @randyzwitch I still can't figure out a clear way to import Report Threads. Like others have said, using from streamlit.ReportThread import add_report_ctx only leads to an error saying that this module doesn't exist :/

dpinol commented 1 year ago

@tvst @randyzwitch I still can't figure out a clear way to import Report Threads. Like others have said, using from streamlit.ReportThread import add_report_ctx only leads to an error saying that this module doesn't exist :/

Now you need to use from streamlit.runtime.scriptrunner import add_script_run_ctx https://github.com/streamlit/streamlit/issues/1326

MoritzNekolla commented 1 year ago

"TLDR: you either need to use the ReportThread class for your thread, or call add_report_ctx on your thread after creation"

But why is the code snippet below not working. Could you explain how to get that to run @tvst or @dpinol ?

import time
import streamlit as st
from threading import Thread
from streamlit.runtime.scriptrunner import add_script_run_ctx

def target():
    time.sleep(1)
    st.text("thread")

t = Thread(target=target)
add_script_run_ctx(t)
t.start()
VEADER1005 commented 1 year ago

@MoritzNekolla Why am I getting "TypeError: Protocols cannot be instantiated" any clue?

MoritzNekolla commented 1 year ago

Why am I getting "TypeError: Protocols cannot be instantiated" any clue?

This is caused by python 3.9.7. Try upgrad/downgrade your python version.

jokester commented 1 month ago

What should be in the document then? I have a few arguable points though:

  1. explain how is user's code executed in absence of custom threads (if it's not introduced elsewhere)
  2. ScriptRunContext, its lifetime, and the missing ScriptRunContext warning
  3. how to do custom threading without ScriptRunContext
  4. how to do custom threading with ScriptRunContext

3 and 4 are separated because I don't think add_script_run_ctx(my_thread) is the go-to fix for missing ScriptRunContext. If I read the code correctly a ScriptRunContext is created for a script run, and contains session-specific binding like UserInfo UploadedFileManager. Using it indifferently can cause memory leak or security issue.