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

Register flask.current_app.teardown_appcontext only once #120

Closed
wants to merge 2 commits into from

Conversation

jsarchibald
Copy link
Contributor

SQL update: only register the shutdown_session function once to the Flask teardown_appcontext_funcs. Checks existing teardown functions before registering it.

…lask teardown_appcontext_funcs. Checks existing teardown functions before registering it.
@jsarchibald jsarchibald requested a review from kzidane June 1, 2020 17:15
@flask.current_app.teardown_appcontext
def shutdown_session(exception=None):
if hasattr(flask.g, "_connection"):
flask.g._connection.close()
Copy link
Member

@kzidane kzidane Jun 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would make more sense for the flag to be set on the execute function itself such that it could do something like:

def execute(...):
  ...
 
  # Ensure shutdown_session is just added once
  if not execute.teardown_appcontext_added:
    execute.teardown_appcontext_added = True

    # Register shutdown_session on app context teardown    
    @flask_current_app.teardown_appcontext
    def shutdown(...):
      ...

# teardown_appcontext handler is not registered initially
execute.teardown_appcontext_added = False

instead of having to search through the list of teardown_appcontext_funcs repeatedly? Also, fwiw, I don't think function names uniquely identifies a function in this case (i.e., more than one teardown_appcontext handler could have the same name).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an instance variable, teardown_appcontext_added, to the SQL class to do this.

…on, using an instance variable within the SQL class.
@jsarchibald jsarchibald requested a review from kzidane June 2, 2020 14:20
@jsarchibald jsarchibald closed this Jun 4, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants