Skip to content

Commit

Permalink
oops, re-clarify that tweens should not have mutable state
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed May 21, 2014
1 parent b9ac2c3 commit 2c096b0
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions docs/narr/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -995,11 +995,6 @@ A tween is called with a single argument, ``request``, which is the
A tween should return a :term:`response`, usually the one generated by the
downstream Pyramid application.

The tween factory will be shared between requests and is used to create one
tween per-request. Shared mutable state on the factory itself needs to be
carefully handled, and should be avoided unless you are willing to handle
the race conditions that may arise.

You can write the tween factory as a simple closure-returning function:

.. code-block:: python
Expand Down Expand Up @@ -1045,6 +1040,10 @@ method:
return response
You should avoid mutating any state on the tween instance. The tween is
invoked once per request and any shared mutable state needs to be carefully
handled to avoid any race conditions.

The closure style performs slightly better and enables you to conditionally
omit the tween from the request processing pipeline (see the following timing
tween example), whereas the class style makes it easier to have shared mutable
Expand Down

0 comments on commit 2c096b0

Please sign in to comment.