Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tvst opened this issue Sep 10, 2019 · 8 comments
Open

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

tvst opened this issue Sep 10, 2019 · 8 comments

Comments

@tvst
Copy link
Contributor

tvst commented Sep 10, 2019

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

@randyzwitch randyzwitch transferred this issue from streamlit/streamlit Oct 14, 2021
@mansidak
Copy link

@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
Copy link

dpinol commented Jun 29, 2023

@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 streamlit/streamlit#1326

@MoritzNekolla
Copy link

"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
Copy link

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

@MoritzNekolla
Copy link

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
Copy link

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.

@msabramo
Copy link

msabramo commented Sep 16, 2024

This is issue 87, was created in 2019, and it's still open? Were these docs added and perhaps the issue was never closed? I ended up here, because I upgraded a library that we use and that library is using threading and now my Streamlit app logs are filled with this message:

missing ScriptRunContext! This warning can be ignored when running in bare mode.

@jokester
Copy link

Created #1154 to document the custom threading stuff (based on my understanding and limited experience)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants