Request attributes vs request environment

17 views
Skip to first unread message

Thierry Florac

unread,
Nov 12, 2020, 8:57:58 AM11/12/20
to pylons-...@googlegroups.com
Hi,
When we have to store custom information about current request, we can use it's properties, attributes or environment (or even annotations).
What is the correct usage of each of them, and is there any benefit of using one of them against the others?

Best regards,
Thierry

Jonathan Vanasco

unread,
Nov 12, 2020, 3:10:02 PM11/12/20
to pylons-discuss
I don't know about "correct", but I use `add_request_method` to attach a custom object(s) with `reify=True,` and then store all the information in those objects.  IIRC, the narrative documentation and tutorials use it for similar purposes.

https://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.add_request_method

Michael Merickel

unread,
Nov 12, 2020, 3:30:02 PM11/12/20
to pylons-...@googlegroups.com
Webob "request" objects are semi-ephemeral in the context of WSGI. Pyramid creates one while processing, and if you're using pyramid_retry then it'll make a new one for each attempt. The "environ", however, is one-per WSGI request and state you put in there will survive for the entire WSGI lifecycle and can be shared with middleware etc that is not Pyramid.

The recommendation is to almost always store data on the request unless you have a really good reason not to.

- Michael

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/344da45e-9eef-436a-9eb9-66e40dda0e56n%40googlegroups.com.

Thierry Florac

unread,
Nov 13, 2020, 5:13:06 AM11/13/20
to pylons-...@googlegroups.com
Very clear!  :)
Many thanks !!
Reply all
Reply to author
Forward
0 new messages