[Twisted-Python] Events in Python
Hi, I have a stackless python app, using twisted in parts (.internet and .adbapi). I need a little help getting pythonic after years of c++ hell. I'd like to use a system of events and observers, like c++ boost.signal. I'd like to be able to subscribe multiple callbacks to a single function and cal them all using something like: event.invoke("some data to send with invocation") I'm thinking twisted callbacks do this: def observe(self, f); self.event.addcallback(f) Are there other ways? Thanks Si -- Linux user #458601 - http://counter.li.org.
Yeah, just keep a list of functions in your own code, no need to use twisted for it and I've not seen twisted functions that really help. something like: class event(object): def __init__(self): self.subscribers=[] def subscribe(self.f): self.subscribers.append(f) def invoke(self,*args,*kwargs): for function in subscribers: function(*args,**kwargs) you can tweak that or whatever, but that's the basic pythonic way to do it in my book(and in my code) a couple options to consider: * in invoke, pass self to the function, I usually do that so I can have functions that do whatever they do to the "event" subclass itself, as in a clearOnFocus function attached to a text control which subclasses event, whenever certain data is seen (type=focusEvent) clear the control. * instead of storing functions store and .callback deffereds, of course those can only be used once so change event.subscribe to event.wait for clarity and reinitialize the list after invoke. -Andy Fundinger On Jan 30, 2008 3:15 PM, Simon Pickles <sipickles@hotmail.com> wrote:
Hi,
I have a stackless python app, using twisted in parts (.internet and .adbapi).
I need a little help getting pythonic after years of c++ hell.
I'd like to use a system of events and observers, like c++ boost.signal.
I'd like to be able to subscribe multiple callbacks to a single function and cal them all using something like:
event.invoke("some data to send with invocation")
I'm thinking twisted callbacks do this:
def observe(self, f); self.event.addcallback(f)
Are there other ways?
Thanks
Si
-- Linux user #458601 - http://counter.li.org.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Blog: http://channel3b.wordpress.com Second Life Name: Ciemaar Flintoff I am a sig Virus. Please put me in your sig so that I can continue to replicate.
Thinking a little further, a DeferredList does this too, keeping a whole group of deferreds and calling them back all at once, I probably should use that more in my own code. Details at ( http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferr... ) - Andy Fundinger On Jan 30, 2008 3:15 PM, Simon Pickles <sipickles@hotmail.com> wrote:
Hi,
I have a stackless python app, using twisted in parts (.internet and .adbapi).
I need a little help getting pythonic after years of c++ hell.
I'd like to use a system of events and observers, like c++ boost.signal.
I'd like to be able to subscribe multiple callbacks to a single function and cal them all using something like:
event.invoke("some data to send with invocation")
I'm thinking twisted callbacks do this:
def observe(self, f); self.event.addcallback(f)
Are there other ways?
Thanks
Si
-- Linux user #458601 - http://counter.li.org.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Blog: http://channel3b.wordpress.com Second Life Name: Ciemaar Flintoff I am a sig Virus. Please put me in your sig so that I can continue to replicate.
Ah yes, deferredLists. thanks for the tip Si Andy Fundinger wrote:
Thinking a little further, a DeferredList does this too, keeping a whole group of deferreds and calling them back all at once, I probably should use that more in my own code. Details at (http://twistedmatrix.com/documents/current/api/twisted.internet.defer.Deferr...)
- Andy Fundinger
On Jan 30, 2008 3:15 PM, Simon Pickles <sipickles@hotmail.com <mailto:sipickles@hotmail.com>> wrote:
Hi,
I have a stackless python app, using twisted in parts (.internet and .adbapi).
I need a little help getting pythonic after years of c++ hell.
I'd like to use a system of events and observers, like c++ boost.signal.
I'd like to be able to subscribe multiple callbacks to a single function and cal them all using something like:
event.invoke("some data to send with invocation")
I'm thinking twisted callbacks do this:
def observe(self, f); self.event.addcallback(f)
Are there other ways?
Thanks
Si
-- Linux user #458601 - http://counter.li.org.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com <mailto:Twisted-Python@twistedmatrix.com> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Blog: http://channel3b.wordpress.com Second Life Name: Ciemaar Flintoff
I am a sig Virus. Please put me in your sig so that I can continue to replicate. ------------------------------------------------------------------------
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Jan 30, 2008 4:18 PM, Simon Pickles <sipickles@hotmail.com> wrote:
Ah yes, deferredLists. thanks for the tip
Just making sure you're not on the wrong track here. Are we talking about recurring events you subscribe to? Remind yourself that Deferreds are a one-shot deal - you can only call them once. -- \\\\\/\"/\\\\\\\\\\\ \\\\/ // //\/\\\\\\\ \\\/ \\// /\ \/\\\\ \\/ /\/ / /\/ /\ \\\ \/ / /\/ /\ /\\\ \\ / /\\\ /\\\ \\\\\/\ \/\\\\\/\\\\\/\\\\\\ d.p.s
Simon Pickles wrote:
Drew Smathers wrote:
Deferreds are a one-shot deal - you can only call them once.
eeek! of course they are! Well at least in Python the roll-your-own option is very straight forward.
Or check out http://pydispatcher.sourceforge.net/. We use this extensively with twisted.... Deferred for one shot events, and dispatcher for events that can be fired multiple times.
At 06:17 AM 1/31/2008, you wrote:
Simon Pickles wrote:
Drew Smathers wrote:
Deferreds are a one-shot deal - you can only call them once.
eeek! of course they are! Well at least in Python the roll-your-own option is very straight forward.
Or check out http://pydispatcher.sourceforge.net/.
We use this extensively with twisted.... Deferred for one shot events, and dispatcher for events that can be fired multiple times.
Another Event Dispatcher may be found here, deep in the bowels of Twisted: http://twistedmatrix.com/documents/current/api/twisted.words.xish.utility.Ev...
Drake Smith wrote:
Another Event Dispatcher may be found here, deep in the bowels of Twisted:
http://twistedmatrix.com/documents/current/api/twisted.words.xish.utility.Ev...
Thanks, I'll use it, I like the idea of not introducing TOO many dependancies. Who knows what other gems lurk in the bowels of twisted? ;) Simon
On Thu, 31 Jan 2008 16:12:05 +0000, Simon Pickles <sipickles@hotmail.com> wrote:
Drake Smith wrote:
Another Event Dispatcher may be found here, deep in the bowels of Twisted:
http://twistedmatrix.com/documents/current/api/twisted.words.xish.utility.Ev...
Thanks, I'll use it, I like the idea of not introducing TOO many dependancies.
Who knows what other gems lurk in the bowels of twisted? ;)
Be ware of things found in bowels. Jean-Paul
Toby Dickenson wrote:
Simon Pickles wrote:
Drew Smathers wrote:
Deferreds are a one-shot deal - you can only call them once.
eeek! of course they are! Well at least in Python the roll-your-own option is very straight forward.
Or check out http://pydispatcher.sourceforge.net/.
We use this extensively with twisted.... Deferred for one shot events, and dispatcher for events that can be fired multiple times.
Actually, the latest incarnation of PyDispatcher is Louie: http://louie.berlios.de/ ... which offers more features and works fine as a drop-in replacement for PyDispatcher. Steve
participants (7)
-
Andy Fundinger
-
Drake Smith
-
Drew Smathers
-
Jean-Paul Calderone
-
Simon Pickles
-
Stephen Waterbury
-
Toby Dickenson