Search code examples
pythondjangoapache2amazon-web-serviceswebfaction

Global variable not working in Amazon Web Service


I got two handlers in Amazon Web Service. I want to share some data in both so i used global variable. where one is used to fetch some value and put it to the global variable when another handler called so is can use same value what is set in global variable, but it is giving default value in another handler and i have checked it is setting the value but in other one it is not giving same it is giving default value. One more imp thing is same code is runnning perfect in webfactional but same code is not running in AWS. Pls help what i have to do and thanks in advance. Here ma code is:

ipid_global = 0
uhid_global = 0

def patient_search:

     global ipid_global
     global uhid_global

     patient  = PatientInfo.objects.get(ip_id__iexact=ip_id)
     dis_advice  = PatientAdvice.objects.get(ip_id__iexact=ip_id)

     ipid_global = int(patient.ip_id)
     uhid_global = str(patient.uh_id)



def patient(request):

        global ipid_global
        global uhid_global

        ip_id = ipid_global
        uh_id = uhid_global

        return HttpResponse(ipid_global)
In real it is indended properly...

Solution

  • Never do this.

    In production, Django (I presume this is Django, although you don't say so - I've added it to the tags) runs in a multi-process environment. Each process has its own copy of the global variables. There is no way to ensure that subsequent requests are served by the same process.

    If you need to keep state between requests, save it in the session, or pass it explicitly as a URL parameter.