在Apache的web.py全局变量的使用 [英] Global variable usage with web.py in Apache

查看:612
本文介绍了在Apache的web.py全局变量的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所遇到的一个奇怪的问题,当我配置我的web.py code与Apache。
    我有3个变量,我需要在2个班使用。我用来处理这种使用全局变量,但遗憾的是,它现在无法正常工作。

I have come across a strange problem when I configured my web.py code with Apache. I have 3 variable which I need to use across 2 classes. I used to handle this using global variables but unfortunately that doesn't work now.

例如:

   urls = (
              '/', 'index',
              '/result','Result' 
   )
   # index is basically a form which takes some inputs     
   class index:
        def POST(self):
                global VAR1, VAR2, VAR3
                 i = web.input()
                 VAR1 = i.word1.__str__()
                 VAR2 = i.word2.__str__()
                 VAR3 = i.word3.__str__()
                 raise web.seeother('/result')
   class Result:
        def GET(self):
                print VAR1, VAR2
                return r_result(VAR1, VAR2)
        def POST(self):
                print VAR2, VAR3

当我独立运行code(即蟒蛇webappy.py),但在Apache设置中使用时,它提供这工作完全正常:

This works perfectly fine when I run the code independently (i.e. python webappy.py) but when used in the apache settings it gives:

NameError:全局名称'VAR1'不Result.Get在打印语句定义

NameError: global name 'VAR1' is not defined at the print statement in Result.Get

我被检查ApplicationIssues:的http:// code。 google.com/p/modwsgi/wiki/ApplicationIssues 并发现了下面的语句。

I was checking the ApplicationIssues: http://code.google.com/p/modwsgi/wiki/ApplicationIssues and found out following statement.

应用全局变量

<青霉>因为它承载一个WSGI应用Python的子帧间preTER被保留在请​​求之间内存,任何全局数据是有效的持久性,并且可以用于从一个请求到下一​​个向前携带状态。在UNIX系统上但是,Apache将通常使用多个进程来处理请求和每个这样的进程都将拥有自己的全球数据。
这意味着,尽管全局数据都可以使用,它只能用于缓存可以是单一的过程的上下文中被安全地重复使用的数据。不能使用全局数据作为保持信息的手段必须是可见的任何请求处理,无论哪个进程与运行。的

我要跨类和函数传递这些变量。
我试图追加的变量的内置&安培;网络模块,但它没有任何锻炼。

I have to pass those variables across the classes and functions. I tried appending the variables to builtin & web module but it didn't workout either.

PS:此外,我不希望这些变量存储在文件或数据库

PS: Also I don't want to store these variables in files or db.

我希望我自己清楚。

推荐答案

您不应该开发Web应用程序时依靠全局变量,因为在某些时候它可以被配置成在不会分享这些变量单独的进程中运行

You shouldn't rely on global variables when developing web application, because at some point it may be configured to run in separate processes that won't share these variables.

要保持它们之间的请求,你应该保存和持久性存储加载它们,所以我想它不可能不使用数据库或类似的解决方案。

To keep them between requests you should save and load them from persistent storage so I guess its not possible without using database or similar solution.

一个很好的方式来加载和保存他们会使用应用处理器将加载这些变量web.ctx这样你就可以在控制器方法访问它们。

A good way to load and save them would be by using application processors that will load these variables in web.ctx so you can access them in controller methods.

例如:

def global_variables_processor(handle):
    # load variables from persistent storage and save them in web.ctx.global_variables
    try:
        return handle()
    finally:
        # save variables from web.ctx.global_variables in persistent storage

app = web.application(urls, globals()
app.add_processor(global_variables_processor)

这篇关于在Apache的web.py全局变量的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆