Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dose Django or wsgi app init multi times in deployment env?

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?

example:

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

UPDATE

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.

like image 479
PaleNeutron Avatar asked Dec 28 '25 11:12

PaleNeutron


1 Answers

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.

like image 76
Joe Avatar answered Dec 31 '25 11:12

Joe