Hi, application block global may cause some probelm, such as app's
performance etc.
before, in other web framework there is simple solution, such as asp.net's
Application['xxx']=xxx

but web.py, with your filter, you can do it, but i thought solution beside
may sth awkward:

import web

def add_global_hook():
    g = web.storage({"counter": 0})
    def _wrapper(handler):
        web.ctx.globals = g
        return handler()
    return _wrapper

class Test:
    def GET(self):
        web.ctx.globals.counter += 1
        return "<h1>Counter: %d</h1>" % web.ctx.globals.counter

if __name__ == '__main__':
    urls = ("/", "Hello")
    app = web.application(urls, globals())
    app.add_processor(add_global_hook())
    app.run()



2009/1/2 David Montgomery <[email protected]>

> Hi,
>
> So, how do I use web.ctx to e.g. set a global dict called ts_dict?
> Assume that I have only a def index class and every time I call index
> I add to the dictionary.  When I start the server the dict should be
> {}, add elements to the dict then upon a condition reset dict to {}.
>
> Thanks
>
>
> 2009/1/1 hhsuper <[email protected]>:
> > yeah, use web.ctx set global variable within a request, not between
> > multi-request(user session do it), also not for the application global
> >
> > On Fri, Jan 2, 2009 at 1:20 AM, Aaron Swartz <[email protected]> wrote:
> >>
> >> If you want variables to be global to a request, use web.ctx.
> >>
> >> On Jan 1, 2009 5:25 AM, "Monty808" <[email protected]> wrote:
> >>
> >>
> >> Hi,
> >>
> >> I am trying to create two global variable. One is a datetime called
> >> start_time and the other is a dictionary.  For each request I want to
> >> add values to the dictionary and I want to find the difference between
> >> the start_time var and the current tme.
> >>
> >>
> >> I have a if condition that will 1) set the start_time to the current
> >> time if a number of seconds pass OR reset the dictionary to {} i.e.
> >> empty.
> >> Question, how to I create global vars for the start_time and the
> >> dictionary in webpy?  When I try to set the current time to start_time
> >> or try to empty the dictionary I get errors about that the global vars
> >> don't exist.
> >>
> >> Thanks
> >>
> >>
> >>
> >>
> >
> >
> >
> > --
> > Su zhaohui   苏召辉
> >
> > >
> >
>
> >
>


-- 
Su zhaohui   苏召辉

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to