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
st.experimental_rerun() raises exception if run in thread #5588
Comments
Hey, @blipk, thank you for opening this issue. I can confirm that CC: @tconkling |
I did some research and I'm not really sure it's possible to raise the The best I could find was to do something like below, but that still requires polling the class PropagatingThread(threading.Thread):
def run(self):
self.exc = None
try:
self.ret = self._target(*self._args, **self._kwargs)
except RerunException as e:
self.exc = e
except BaseException as e:
self.exc = e
def join(self, timeout=None):
super(PropagatingThread, self).join(timeout)
if self.exc:
raise self.exc
return self.ret Adding the context does give me correct access to Right now as an alternative, I am writing to a token .py file at the end of the thread, and import that file in my main page and have |
Hey @blipk - yeah, as you've noted, this won't work when called from another thread. None of Streamlit's APIs are safe to call from any thread other than your script's main thread; code that does so has ventured into the realm of undefined behavior, and we strongly recommend against it :) If you need to run a separate thread that invokes Streamlit APIs, you'll need to do something like pushing requests to a shared queue that's processed by your script thread. (This isn't something that's particularly easy to do with Streamlit, unfortunately. But we do have some - still vague - plans to make more complex use cases, like this one, less tricky!) I'm closing this issue because Streamlit is working as expected, but feel free to re-open if I've misunderstood! |
Checklist
Summary
Calling
st.experimental_rerun()
in a thread raises an exception and doesnt rerun streamlit.Looking at the source for
experimental_rerun()
it seems it raises the exception in any case, and this is the method by which it signals the rerun, being caught by the main thread.I'm not sure which part of streamlit handles this, but I am passing the correct context manager to the thread so I figured there should be a way to also catch the exception from a thread.
This is the error that is raised is provided below.
Reproducible Code Example
Steps To Reproduce
streamlit run main.py
on the provided example.Expected Behavior
The context manager is aware of the (intended?) exception in the thread and Streamlit is rerun at the end of the thread.
Current Behavior
Is this a regression?
Debug info
Additional Information
No response
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: