There are no app level vars in web2py. The reason is that one app may be 
served by multiple threads and/or multiple processes running on multiple 
machines. How do you keep it in sync? You can have a global constant 
(define it in models/0.py) or you can have a variable computed and cached.

On Saturday, 4 May 2013 21:11:19 UTC-5, Fabiano wrote:
>
> How can I have app-wise global var in web2py? 
>
> As the models, controllers and views of web2py are executed for every 
> request, I cannot set a var on the top level scope of these files as 
> that assignment will be re-executed every request and reset the value. 
>
> Testing if the variable is already set in the top level scope and 
> setting it conditionally doesn't work either. Nor does it using 
> globals(), which I think is the same actually. 
>
>
> One way I found to do it is to set a var on the top level scope of a 
> custom module, but I don't know if my code can depend on this 
> behavior, as it seems this was changed when import_local() was 
> deprecated and it is undocumented. 
>
> Also I am not sure if this would be reliable depending on the 
> deployment method. For example, it obviously wouldn't work on a bare 
> cgi deploy, as the entire framework get executed every request. But I 
> consider bare cgi a corner case, for MVC we should always have a long 
> running process. What about other deploys, like wsgi on apache? 
>
> Another consideration is that it seems this approach wouldn't be 
> global per app but global per web2py instance -- this is not ideal but 
> could be circumvented by a name scoping mechanism, like using the app 
> name. (Even then, I my case it could be a security issue). 
>
> In some cases it would be possible to just store the value in a file 
> (like in the private folder) or in the database, but then it would be 
> persistent and not really a variable in the long running app. 
>
> The better alternative I found so far is to use cache.ram to store an 
> object and set time_expire=None. I played a bit with this option and 
> it seems to work just fine for my purposes. 
>
> Are there other/preferred way to have an app level variable in memory? 
>
> In case you are curious about my use case: I want to store an 
> encryption key in RAM memory only for as long as the app is running, 
> this key cannot go to persistent storage. When the app is started all 
> controllers that depend on this key will report an error until an 
> administrator logs into the app and manually enters a passphrase 
> (which should be stored only in administrator's brain), the app 
> derives the encryption key from the passphrase and keeps it into the 
> server's RAM memory as long as the app is running. 
>
>
> Fabiano. 
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to