Dyno Fu wrote:
> what i wantta do is to share some data in memory
which is shared
> between each session. or in other words it's scope is
> application level
> rather than session level.
>
> class Root:
> def __init__(self):
> #where to put this data to share between
each
> session/request?
> #something like
cherrypy.appContex.put("data",
> self.data)
> self.data = "somedata" #may be
a large list or dict
>
> self.plot = Plot()
> self.plot.expose = True
> ...
> class Plot:
> def __init__(self):
> #how to get the data from application context?
> self.data = ? #something like
> cherrypy.appContext.get("data")
> ...
>
> i know, i can init the data and pass around with
parameter, for e.g.
> self.plot = Plot(data)
>
> i am here to seek a more elegance way in cherrypy, as
what we can do
> with Java Servlet -- there is a Web Context called
> "javax.servlet.ServletContext" scope, and we
can simply store and
> retrieve the data in application level. any suggestion?
I don't see how
javax.servlet.ServletContext.put("data", foo) is
"more
elegant" than self.data = foo <wink>. But it's
much easier in CP--stick
attributes into cherrypy.root (in CherryPy 2.x; use
cherrypy.request.app
in CherryPy 3). I wouldn't even bother adding such items to
each object;
just refer to cherrypy.root throughout your code:
class Root:
def __init__(self):
...
class Plot:
def handler(self, arg):
if arg:
root.data = arg
return root.data
root = Root()
cherrypy.tree.mount(root)
root.data = "somedata"
Similarly, if you wanted some data to be request-scoped,
stick it into
the cherrypy.request object. Engine-scoped, stick it into
cherrypy.engine.
Robert Brewer
System Architect
Amor Ministries
fumanchu amor.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users googlegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribe googlegroups.com
For more options, visit this group at http://groups-beta.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|