1

please I need some help, I have a web application with classic functionalities for users management (account creation, login,...). my problem is that I am trying to creat an instance of an object "UserObject" (not serializable object) for each user when he connects (login), this object will be used to process user requests in some views, so the object must be accessible from any view, for that i have a global dictionary "users_objects" in view.py that contains all users objects (the dictionary key is the user name and the value is the "UserObject" object), so view.py look lik this :

from user_object import UserObject

users_objects = {}

def login(request):

    //login control and creation of session and context
    ......
    ......
    global users_objects
    user_name = request.session['name']
    users_objects[user_name] = UserObject()
    return render(request, 'mySite/home.html', context)

def request_view(request):

    param = request.GET.get('parameter', None)
    global users_objects
    user_name = request.session['name']
    obj = users_objects[user_name]
    res = obj.process(param)
    return HttpResponse(str(res))

This approach work fine with django dev-server, but when I configure django with a real production server (apache) the content of the global dictionary "users_objects" disappear and I get an empty dictionary. please, did anyone know why does this happen ?? and what is the best solution to use a global dictionary in django ?? thank you in advance

1 Answer 1

2

The reason this is happening is because mod_wsgi is running your app with multiple processes, each of which have their own variable space.

But there is no good reason to do this. Data is stored in the database, don't put it in global objects.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you for your response, The "UserObject" object is a not serializable object , furthermore when I instantiate a new "UserObject" a connection to a remote server is established and i need to keep this connection alive to process users requests, so I can't store the object in the database
Well, this can't work. In a multi-process environment there is absolutely no guarantee you will get the same process that originally created your object. You need to rethink your structure; you will probably need to save the data in the local database and establish a short-lived connection with the remote server each time you need to process something.
A quick way around it, but not a long term solution as Daniel explains, is to ensure you use daemon mode of mod_wsgi. That uses one process. Daemon mode is also the preferred deployment mechanism. You should not be using the default embedded mode. blog.dscpl.com.au/2012/10/…
Thank you for your response Graham its exactly what i need, just for demo purpose.
So if apache is running with multiple processes does that mean that each process gets separate instance of the django project?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.