Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Django global variables shared across all users or individual users?

Hey this is a more general question.

The first part is just knowing exactly how Global variables work. If multiple users are accessing the server at the same time is a global variable going to be shared across all of the users? or will each user have their own instance of that global variable?

I am aware of sessions and how this is probably the best answer to solve my issue, however, I am currently working with the Django FormWizard and it doesnt seem to have access to request so I am unable to use the sessions. I am not entirely sure how to access request so if anyone knows how to do that I appreciate the help.

Thanks!

like image 972
Midi_ Avatar asked Oct 16 '25 18:10

Midi_


1 Answers

Depends on how you deploy your application. Gunicorn for example by default will load every worker process with its own environment, but with --preload it will load app and only after that prefork workers. The second way global variables will be shared, but with a limitation: all shared variables will copy-on-write, so if you try to modify global variable in a worker process, that variable will be copied and you'll modify copy of instance.

Answering your second question. You cannot get request instance from anywhere you want in Django if it's not directly passed into function. Modifying global variables often isn't threadsafe, be aware.

like image 96
Mikhail Pyrev Avatar answered Oct 19 '25 13:10

Mikhail Pyrev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!