Set your variable to session: >>> session.my_var = "my_content"
http://web2py.com/books/default/chapter/29/03#Let's-count ;-) On Sat, May 4, 2013 at 11:11 PM, Fabiano Engler <fabianoeng...@gmail.com> 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 web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- --- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.