1

I am referring to the answer from this post, where it is proposed that sessions can be used to store global state per-user: Are global variables thread safe in flask? How do I share data between requests?

Specifically about sessions, when flask application is configured to run on multiple processes, then is session shared across processes?

Example: If 1 URL request hits process A, and the next request from the same user hits process B, then will the session set when the request hit process A be available in the process B?

variable
  • 8,262
  • 9
  • 95
  • 215
  • You are scaring me as well... Isn't it literally multi-**process**es? not multi-**thread**s? – ghchoi Nov 04 '19 at 07:53
  • That question is about across threads. But I am asking about across process. Is it duplicate? – variable Nov 04 '19 at 08:02
  • Good point, but the answer shares the relevance of the cookie-based session data where by default, session is stored client-side and isn't affected by underlying server configuration. See (possible) answer below. – djnz Nov 04 '19 at 08:35

1 Answers1

2

If I understand correctly, the request context is specific to a worker, which can be a thread/process/coroutine, means session is not shared and is set per request:

The docs:

Because a worker (thread, process, or coroutine depending on the server) handles only one request at a time, the request data can be considered global to that worker during that request. Flask uses the term context local for this.

djnz
  • 2,021
  • 1
  • 13
  • 13
  • That goes against the concept of sessions that we have known. As I expect it to be shared for all requests for the user. Atleast within the same process. And hopefully across processes. What you have mentioned - per request - are the objects like g, current_app, request. – variable Nov 04 '19 at 09:27
  • My interpretation is that at the server level, each request is it's own worker, which has it's own process. Each request is (what I assume) sent with the session data and that data becomes global for that worker. In the doc I linked the "Lifetime of the context" gives a bit more detail, but there is still some missing detail which would answer your question directly. – djnz Nov 04 '19 at 20:01