Re: data from bus to publishing function

IngoognI <[email protected]> Sun, 22 Jan 2017 05:12:32 -0800 (PST)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
Well well, what about that:

@cherrypy.expose
def pubcpu(self):
    class Var(object):
        e = threading.Event()
        disconnected = 0
        
        @property
        def val(self):
            return self._val

        @val.setter
        def val(self, value):
            self.e.set()
            self._val = value
            self.disconnected += 1

        def wait(self):
            self.e.wait()
        def clear(self):
            self.e.clear()

        def publish(self):
            while True:
                self.e.wait()
                self.disconnected = 0
                yield self._val
                self.e.clear()
            
    cherrypy.response.headers["Content-Type"] = "text/event-stream"
    k = Var()
    def value(v):
        print("\n Value: ",v)
        k.val = v
        if k.disconnected > 1:
            cherrypy.engine.unsubscribe("cpu", value)

    cherrypy.engine.subscribe("cpu", value)            
    def pub():
        for val in k.publish():
            yield "event: time\n" + "data: " + str(val) + "\n\n"
    return pub()        
pubcpu._cp_config = {'response.stream': True}


Ingo


-- 
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.