2

According to the Flask 0.12 document (emphasis mine):

The application context is created and destroyed as necessary. It never moves between threads and it will not be shared between requests...

However, the relevant part of the source code seems a bit strange to me:

# Before we push the request context we have to ensure that there
# is an application context.
app_ctx = _app_ctx_stack.top
if app_ctx is None or app_ctx.app != self.app:
    app_ctx = self.app.app_context()
    app_ctx.push()
    self._implicit_app_ctx_stack.append(app_ctx)
else:
    self._implicit_app_ctx_stack.append(None)

As you can see, when app_ctx is None or app_ctx.app != self.app is False, nothing is done except None is appended to self._implicit_app_ctx_stack, so I suspect the existing application context is re-used, i.e. shared between requests.

To make things even more complicated, this experiment also shows that application context is preserved between requests (because after the first request, g.foo still contains xzy).

That said, the document is inconsistent with both the source code and an experiment. So how on earth does Flask handle a coming request, when there is an existing application context?

nalzok
  • 14,965
  • 21
  • 72
  • 139

0 Answers0