Added notes on proxies
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent c0abdc4 · Browse files · 2 files·+63 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+63 added−18 removed
- other:+63 added−18 removed
Provenance
- Commit
3b0eb0f- Tree
ea85b91- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c0abdc4
2 files changed+63 added−18 removed
modified:docs/api.rst+35 added
36 unmodified lines37 access to it through that global object. Internally Flask makes38 sure that you always get the correct data for the active thread if you39 are in a multithreaded environment.40 41+ This is a proxy. See :ref:`notes-on-proxies` for more information.42+43 The request object is an instance of a :class:`~werkzeug.Request`44 subclass and provides all of the attributes Werkzeug defines. This45 just shows a quick overview of the most important ones.46 118 unmodified lines165 166 The session object works pretty much like an ordinary dict, with the167 difference that it keeps track on modifications.168 169+ This is a proxy. See :ref:`notes-on-proxies` for more information.170+171 The following attributes are interesting:172 173 .. attribute:: new174 34 unmodified lines209 210 Just store on this whatever you want. For example a database211 connection or the user that is currently logged in.212 213+ This is a proxy. See :ref:`notes-on-proxies` for more information.214 215+216 Useful Functions and Classes217 ----------------------------218 219 .. data:: current_app1 unmodified line221 Points to the application handling the request. This is useful for222 extensions that want to support multiple applications running side223 by side.224 225+ This is a proxy. See :ref:`notes-on-proxies` for more information.226+227 .. autofunction:: url_for228 229 .. function:: abort(code)230 165 unmodified lines396 It is sent *before* the standard exception handling kicks in and even397 in debug mode, where no exception handling happens. The exception398 itself is passed to the subscriber as `exception`.399 400+.. currentmodule:: None401+402 .. class:: flask.signals.Namespace403 404 An alias for :class:`blinker.base.Namespace` if blinker is available,405 otherwise a dummy class that creates fake signals. This class is7 unmodified lines413 do nothing but will fail with a :exc:`RuntimeError` for all other414 operations, including connecting.415 416 .. _blinker: http://pypi.python.org/pypi/blinker417+418+.. _notes-on-proxies:419+420+Notes On Proxies421+----------------422+423+Some of the objects provided by Flask are proxies to other objects. The424+reason behind this is, that these proxies are shared between threads and425+they have to dispatch to the actual object bound to a thread behind the426+scenes as necessary.427+428+Most of the time you don't have to care about that, but there are some429+exceptions where it is good to know that this object is an actual proxy:430+431+- The proxy objects do not fake their inherited types, so if you want to432+ perform actual instance checks, you have to do that on the instance433+ that434+- if the object reference is important (so for example for sending435+ :ref:`signals`)436+437+If you need to get access to the underlying object that is proxied, you438+can use the :meth:`~werkzeug.LocalProxy._get_current_object` method::439+440+ app = current_app._get_current_object()441+ my_signal.send(app)modified:docs/signals.rst+28 added-18 removed
39 unmodified lines40 the optional second argument specifies a sender. To unsubscribe from a41 signal, you can use the :meth:`~blinker.base.Signal.disconnect` method.42 43 For all core Flask signals, the sender is the application that issued the44-signal. This however might not be true for Flask extensions, so consult45-the documentation when subscribing to signals.44+signal. When you subscribe to a signal, be sure to also provide a sender45+unless you really want to listen for signals of all applications. This is46+especially true if you are developing an extension.47 47-Additionally there is a convenient helper method that allows you to48-temporarily subscribe a function to a signal. This is especially helpful49-for unittests (:meth:`~blinker.base.Signal.temporarily_connected_to`).50-This has to be used in combination with the `with` statement.51-48 Here for example a helper context manager that can be used to figure out49 in a unittest which templates were rendered and what variables were passed50 to the template::51 52 from flask import template_rendered53 from contextlib import contextmanager54 55 @contextmanager60- def captured_templates():56+ def captured_templates(app):57 recorded = []58 def record(template, context):59 recorded.append((template, context))64- template_rendered.connect(record)60+ template_rendered.connect(record, app)61 try:62 yield recorded63 finally:68- template_rendered.disconnect(record)64+ template_rendered.disconnect(record, app)65 66 This can now easily be paired with a test client::67 72- with captured_templates() as templates:68+ with captured_templates(app) as templates:69 rv = app.test_client().get('/')70 assert rv.status_code == 20071 assert len(templates) == 172 template, context = templates[0]73 assert template.name == 'index.html'74 assert len(context['items']) == 1075 80-All the template rendering in the code, the `with` block wraps will now be81-recorded in the `templates` variable. Whenever a template is rendered,82-the template object as well as context is appended to it.76+All the template rendering in the code issued by the application `app`77+in the body of the `with` block will now be recorded in the `templates`78+variable. Whenever a template is rendered, the template object as well as79+context are appended to it.80 81+Additionally there is a convenient helper method82+(:meth:`~blinker.base.Signal.temporarily_connected_to`). that allows you83+to temporarily subscribe a function to a signal with is a context manager84+on its own which simplifies the example above::85+86+ from flask import template_rendered87+88+ def captured_templates(app):89+ recorded = []90+ def record(template, context):91+ recorded.append((template, context))92+ return template_rendered.temporarily_connected_to(record, app)93+94 Creating Signals95 ----------------96 97 If you want to use signals in your own application, you can use the64 unmodified lines162 template.name or 'string template',163 context)164 165 from flask import request_started156- request_started.connect(log_template_renders)166+ request_started.connect(log_template_renders, app)167 168 .. data:: flask.request_started169 :noindex:170 7 unmodified lines178 def log_request(sender):179 sender.logger.debug('Request context is set up')180 181 from flask import request_started172- request_started.connect(log_request)182+ request_started.connect(log_request, app)183 184 .. data:: flask.request_finished185 :noindex:186 6 unmodified lines193 sender.logger.debug('Request context is about to close down. '194 'Response: %s', response)195 196 from flask import request_finished187- request_finished.connect(log_response)197+ request_finished.connect(log_response, app)198 199 .. data:: flask.got_request_exception200 :noindex:201 7 unmodified lines209 def log_exception(sender, exception):210 sender.logger.debug('Got exception during processing: %s', exception)211 212 from flask import got_request_exception203- got_request_exception.connect(log_exception)213+ got_request_exception.connect(log_exception, app)214 215 .. _blinker: http://pypi.python.org/pypi/blinker