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

Be able to close cached resources (@st.cache_resource) #8674

Open
2 tasks done
gerbenoostra opened this issue May 15, 2024 · 1 comment
Open
2 tasks done

Be able to close cached resources (@st.cache_resource) #8674

gerbenoostra opened this issue May 15, 2024 · 1 comment
Labels
feature:cache Related to st.cache_data and st.cache_resource type:enhancement Requests for feature enhancements or new features

Comments

@gerbenoostra
Copy link

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

@st.cache_resource is recommended to use for non-hashable resources like database connections. However, such resources might require closing.

Why?

@st.cache_resource is recommended to use for non-hashable resources like database connection. From the doc:

 @st.cache_resource
 def get_database_session(_sessionmaker, url):
     # Create a database connection object that points to the URL.
     return connection

However, these resources could require closing (closing the database connection, stopping a SparkSession).

In the current implementation I'm not able to close the connection, requiring the streamlit instance to be restarted. For database connections this will result in dangling connections.

How?

Ideally I'd be able to close the resource in the same function similar to PyTest yields usage:

 @st.cache_resource
 def get_database_session(_sessionmaker, url):
     # Create a database connection object that points to the URL.
     yield connection
     connection.close()

Alternatively, perhaps a close callback:

 @st.cache_resource(close= lambda con: con.close())
 def get_database_session(_sessionmaker, url):
     # Create a database connection object that points to the URL.
     return connection

Additional Context

No response

@gerbenoostra gerbenoostra added the type:enhancement Requests for feature enhancements or new features label May 15, 2024
Copy link

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@LukasMasuch LukasMasuch added the feature:cache Related to st.cache_data and st.cache_resource label May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:cache Related to st.cache_data and st.cache_resource type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

No branches or pull requests

2 participants