Skip to content Skip to sidebar Skip to footer

Global Variable Usage With Web.py In Apache

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 va

Solution 1:

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.

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.

For example:

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

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

Solution 2:

Well, there's a possible reason in this excerpt from your question...

Apache will normally use multiple processes to handle requests and each such process will have its own global data.

...so your code will only work reliably if you've configured apache to use a single process which never terminates, with the directives...

MaxClients 1
MaxRequestsPerChild 0

If this isn't a practical option, you'll have to store those variables somewhere else.

Post a Comment for "Global Variable Usage With Web.py In Apache"