I mean, if I store a global int in a Django project's memory and modify/view it, this is OK with manage.py runserver.
However, would this still work in deployment environment?
I am not sure how production web server(apache or uwsgi) will use my code. Will this app initialed many times in different processes?
global_var.py:
command = CommandEvent("start") #a class contains event and command
var1 = 1
views.py:
from global_var import var1
def show_var(request):
return var1
I store data in memory because I forked another thread to grab data from other source. I have to control and get data from this thread with view functions.
spider_py:
from global_var import var1, command
spider_thread = threading.Thread(target=spider_serve_forever, args=(command, var1))
def spider_serve_forever(command, var1):
while(1):
if command.str == "start":
pass
elif command.str == "get_data":
var1 = get_data()
command.event.set()
else:
pass
I have another thread wait for event, once set, push a notification through websocket to the web-client.
The typical production configuration for a Django app using any WSGI server involves spawning a certain number of processes, each with a certain number of threads. Exactly what those numbers are depends on what web server and/or WSGI server is being used, but a rule of thumb that many people use is to configure things such that there is at least one process per server CPU.
I would assume any deployment of your Django app will be multiprocess, so any trick that assumes something in memory is consistent across multiple requests will not work, because you don’t know which process will handle it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With