python - Global variable usage with web.py in Apache -


i have come across strange problem when configured web.py code apache. have 3 variable need use across 2 classes. used handle using global variables unfortunately doesn't work now.

example:

   urls = (               '/', 'index',               '/result','result'     )    # index form takes inputs         class index:         def post(self):                 global var1, var2, var3                  = 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 

this works fine when run code independently (i.e. python webappy.py) when used in apache settings gives:

nameerror: global name 'var1' not defined @ print statement in result.get

i checking applicationissues: http://code.google.com/p/modwsgi/wiki/applicationissues , found out following statement.

application global variables

because python sub interpreter hosts wsgi application retained in memory between requests, global data persistent , can used carry state forward 1 request next. on unix systems however, apache use multiple processes handle requests , each such process have own global data. means although global data can used, can used cache data can safely reused within context of single process. cannot use global data means of holding information must visible request handler no matter process runs in.

i have pass variables across classes , functions. tried appending variables builtin & web module didn't workout either.

ps: don't want store these variables in files or db.

i hope made myself clear.

you shouldn't rely on global variables when developing web application, because @ point may configured run in separate processes won't share these variables.

to keep them between requests should save , load them persistent storage guess not possible without using database or similar solution.

a way load , save them using application processors load these variables in web.ctx can access them in controller methods.

for example:

def global_variables_processor(handle):     # load variables persistent storage , save them in web.ctx.global_variables     try:         return handle()     finally:         # save variables web.ctx.global_variables in persistent storage  app = web.application(urls, globals() app.add_processor(global_variables_processor) 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -