全局变量在 Amazon Web Service 中不起作用

发布于 2024-11-29 22:31:32 字数 785 浏览 8 评论 0原文

我在 Amazon Web Service 中有两个处理程序。我想在两者中共享一些数据,所以我使用了全局变量。其中一个用于获取一些值并将其放入全局变量,当另一个处理程序调用时,它可以使用全局变量中设置的相同值,但它在另一个处理程序中给出默认值,我已经检查过它正在设置该值但在另一种情况下,它没有给出相同的值,而是给出默认值。另一件事是相同的代码在 webfactional 中运行完美,但相同的代码在 AWS 中不运行。请帮助我做什么并提前致谢。这里的代码是:

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...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2024-12-06 22:31:32

永远不要这样做。

在生产中,Django(我认为这是 Django,尽管你没有这么说 - 我已将其添加到标签中)在多进程环境中运行。每个进程都有自己的全局变量副本。 无法确保后续请求由同一进程提供服务。

如果您需要在请求之间保留状态,请将其保存在会话中,或将其作为 URL 参数显式传递。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文