Skip to content

pass context internally instead of using contextvars #5815

@davidism

Description

@davidism

Currently, there are a bunch of different methods on the Flask class that run to dispatch each request. Many of these access request and other context proxies. We should update them to pass the AppContext everywhere instead. This is more convenient after #5812 with only one context object instead of two. As @pgjones pointed out in #5229, not having to access the contextvar is a significant speedup to ASGI Quart, although it doesn't appear to affect WSGI Flask as much. Perhaps if we were serving with greenlets and so contexts were switching more, it would be more noticeable in Flask too.

The obvious problem is that it is a breaking change to the signatures of all these methods. I'm unsure how many methods are affected, but I'm also unsure how many projects are even subclassing Flask to override any of the methods. The fact that the methods are public seems unintentional, compared to the much more common ways of configuring and customizing an app.

We could continue to support both signatures, showing a deprecation warning for the old one, by adding some detection to Flask.__init__. This would add some amount of time during app setup, but wouldn't affect runtime performance of the app.


Another idea I had for the future was to have @route and other decorators inspect the signature of the decorated function, to allow injecting request, app, etc. if users would rather use that pattern than import the proxies. Having the request object directly available in all the dispatch methods would make this more straightforward. User code would be able to choose what pattern they want, the current proxy pattern would never be deprecated for user code.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions