It was interesting to me and I’ve checked it. As you can read NewRequest subscribers are called 3 times on each request (or 7 times with debug_toolbar enabled…) while BeforeRender subscribers are called 1 time on each request (> 30 times with debug_toolbar enabled).
So, if I want to connect mongodb to my project through NewRequest event it will be done 3 times for each request…
Why is that? Why should server do the same job 3 times on each request?
Thanks in advance!!!
Pyramid does not call
NewRequestmore than once per request. The only reason this would happen is if you are registering your subscriber multiple times accidentally. Another common reason people think it is called multiple times is that the browser usually follows requests with a favicon request, but that only accounts for 2 invocations. Can you show any output or describe your problem more to convince me that the subscriber really is being invoked more than once?BeforeRenderwill be called multiple times (once for every template rendered). When the debug toolbar is enabled there is a lot of stuff being rendered on each request but even then 30 sounds more like 3 times what I would expect.It’s not a good idea to connect to your database in a
NewRequestsubscriber, in general, because that subscriber is invoked for static resources as well (literally all requests). A better pattern is to create a lazy/reified property on therequestobject viaconfig.set_request_property. This will connect the first time you use the database in each request, and have no performance penalty when you do not.