Skip to content
Myr

clean up deferred callback doc

Before adoption: this commit came in with the history the project adopted, so no landing records it.

remove doc about writing after_this_request [ci skip]

authored by David Lord · committed by David Lord · parent 0420cb2 · Browse files · 1 file+23 added−51 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+23 added−51 removed

  • other:+23 added−51 removed

1 file+23 added−51 removed

modified:docs/patterns/deferredcallbacks.rst+23 added-51 removed
1 unmodified line
23Deferred Request Callbacks4==========================56One of the design principles of Flask is that response objects are created7and passed down a chain of potential callbacks that can modify them or8replace them. When the request handling starts, there is no response9object yet. It is created as necessary either by a view function or by10some other component in the system.6One of the design principles of Flask is that response objects are created and7passed down a chain of potential callbacks that can modify them or replace8them. When the request handling starts, there is no response object yet. It is9created as necessary either by a view function or by some other component in10the system.1112But what happens if you want to modify the response at a point where the13response does not exist yet? A common example for that would be a14before-request function that wants to set a cookie on the response object.12What happens if you want to modify the response at a point where the response13does not exist yet? A common example for that would be a14:meth:`~flask.Flask.before_request` callback that wants to set a cookie on the15response object.1616One way is to avoid the situation. Very often that is possible. For17instance you can try to move that logic into an after-request callback18instead. Sometimes however moving that code there is just not a very19pleasant experience or makes code look very awkward.17One way is to avoid the situation. Very often that is possible. For instance18you can try to move that logic into a :meth:`~flask.Flask.after_request`19callback instead. However, sometimes moving code there makes it more20more complicated or awkward to reason about.2121As an alternative possibility you can attach a bunch of callback functions22to the :data:`~flask.g` object and call them at the end of the request.23This way you can defer code execution from anywhere in the application.22As an alternative, you can use :func:`~flask.after_this_request` to register23callbacks that will execute after only the current request. This way you can24defer code execution from anywhere in the application, based on the current25request.262526The Decorator27-------------2829The following decorator is the key. It registers a function on a list on30the :data:`~flask.g` object::3132 from flask import g3334 def after_this_request(f):35 if not hasattr(g, 'after_request_callbacks'):36 g.after_request_callbacks = []37 g.after_request_callbacks.append(f)38 return f394041Calling the Deferred42--------------------4344Now you can use the `after_this_request` decorator to mark a function to45be called at the end of the request. But we still need to call them. For46this the following function needs to be registered as47:meth:`~flask.Flask.after_request` callback::4849 @app.after_request50 def call_after_request_callbacks(response):51 for callback in getattr(g, 'after_request_callbacks', ()):52 callback(response)53 return response545556A Practical Example57-------------------5827At any time during a request, we can register a function to be called at the60end of the request. For example you can remember the current language of the61user in a cookie in the before-request function::28end of the request. For example you can remember the current language of the29user in a cookie in a :meth:`~flask.Flask.before_request` callback::3031 from flask import request, after_this_request3233 @app.before_request34 def detect_user_language():35 language = request.cookies.get('user_lang')3637 if language is None:38 language = guess_language_from_request()3940 # when the response exists, set a cookie with the language41 @after_this_request42 def remember_language(response):43 response.set_cookie('user_lang', language)4445 g.language = language