Managing background thread termination during script cancellation
0 reputation · 19 May 2021, 07:28 UTC
Streamlit scripts execute from top to bottom, and the st.stop() function allows for the immediate cessation of the current execution flow. However, this mechanism primarily halts the script's progression within the Streamlit runtime and does not natively signal termination to external resources.
When developers launch background threads or subprocesses to handle long-running tasks, these processes may continue to execute even after the user triggers a stop or closes the browser session. This behavior can lead to orphaned processes and unnecessary memory consumption, as the framework lacks a built-in cancellation token API to propagate abort signals to asynchronous tasks.
What is the recommended approach for ensuring that background threads are gracefully terminated when a Streamlit script is stopped? Does the current architecture support a mechanism to link thread lifecycles to the session state or the script's execution context?