You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import threading
from streamlit.runtime.scriptrunner import add_script_run_ctx
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx
from streamlit.runtime import get_instance
def heart_beat():
"""
Heartbeat function to track whether the session is alive
"""
thread = threading.Timer(interval=2, function=heart_beat)
# insert context to the current thread, needed for
# getting session specific attributes like st.session_state
add_script_run_ctx(thread)
# context is required to get session_id of the calling
# thread (which would be the script thread)
ctx = get_script_run_ctx()
# this is the main runtime, contains all the sessions
runtime = get_instance()
if runtime.is_active_session(session_id=ctx.session_id):
thread.start()
else:
return
if __name__ == "__main__":
heart_beat()
Is it possible to shut down the session on the backend side?
Why?
It's important to have more controlled behavior, as our application will potentially interact with multiple users.
How?
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered:
Hey @Anna050689! Can you tell us a bit more about why you need to detect when the session is closing? E.g. do you have some things in your code that you need to close or clean up? More detail helps us to figure out how common this use case is and prioritize it.
@sfc-gh-jcarroll this might be a good issue for your hooks idea.
Hello, @jrieke! The code snippet provided in the summary section detects when the user closes the browser tab. In this case, it's possible to detect this action and clean up some artifacts after the user exits. However, the session on the backend remains open. Since we want to support multiple simultaneous users, this could be resource-consuming in terms of memory and other resources.
I would also like to have a bit more granular control over how long AppSessionManager keeps tabs open. A configurable parameter like "websocketSessionTimeout" that would cause a session to be cleaned up if it hasn't heard from the browser in X seconds (would require a browser-heartbeat msg every so often) would be quite useful)
Checklist
Summary
I want to implement the mechanism to detect the user exit after closing the tab and shut down the session on the backend side. I used the solution provided in the streamlit discussion - https://discuss.streamlit.io/t/detecting-user-exit-browser-tab-closed-session-end/62066/2
My code snippet is the next -
Is it possible to shut down the session on the backend side?
Why?
It's important to have more controlled behavior, as our application will potentially interact with multiple users.
How?
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: