Re: data from bus to publishing function

"Jason R. Coombs" <jaraco-aM/[email protected]> Thu, 12 Jan 2017 05:18:18 -0800 (PST)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
Hi Ingo. I think what you're asking is how do you store state. You want to 
retain state across multiple requests, and in this example, have one 
request accept a piece of data and a subsequent request return that piece 
of data.

In the simplest form, you could simply store this value in a global 
variable or an instance variable on the Root instance.

In .sensor, "self.cpu = input_json['cpu']". Then in .pubcpu, 
"print(self.cpu)" or "yield 'event' + str(cpu) + '\n'".

Of course, if you use this technique, the state is stored in memory of the 
Python process, so won't work if you have more than one process serving 
your site. Also, if the set/get operation isn't threadsafe (i.e. it doesn't 
happen atomically), you could run into issues even in a single process. 
However, for this simple example, setting an instance attribute should be 
sufficient.

You can't use the engine pub/sub model to save state across requests. Data 
that's published to the bus is routed to subscribing routines immediately 
and isn't queued for retrieval in another request.

Hope that helps.

-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.