[Twisted-Python] Events in Python
![](https://secure.gravatar.com/avatar/c4c8b1f74e1124bac9349772a7085ffc.jpg?s=120&d=mm&r=g)
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.
![](https://secure.gravatar.com/avatar/1cc7eeef749e51fbcae990805cce634d.jpg?s=120&d=mm&r=g)
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:
-- 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.
![](https://secure.gravatar.com/avatar/1cc7eeef749e51fbcae990805cce634d.jpg?s=120&d=mm&r=g)
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:
-- 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.
![](https://secure.gravatar.com/avatar/d6304567ada7ac5e8c6f4e5902270831.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/41bb9450b145ffb3ceb8148137792ab7.jpg?s=120&d=mm&r=g)
Simon Pickles wrote:
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.
![](https://secure.gravatar.com/avatar/960796decb792386a8652abe32d2236c.jpg?s=120&d=mm&r=g)
At 06:17 AM 1/31/2008, you 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...
![](https://secure.gravatar.com/avatar/182974f8b2562287a54415119be4535c.jpg?s=120&d=mm&r=g)
Toby Dickenson wrote:
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
![](https://secure.gravatar.com/avatar/1cc7eeef749e51fbcae990805cce634d.jpg?s=120&d=mm&r=g)
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:
-- 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.
![](https://secure.gravatar.com/avatar/1cc7eeef749e51fbcae990805cce634d.jpg?s=120&d=mm&r=g)
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:
-- 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.
![](https://secure.gravatar.com/avatar/d6304567ada7ac5e8c6f4e5902270831.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/41bb9450b145ffb3ceb8148137792ab7.jpg?s=120&d=mm&r=g)
Simon Pickles wrote:
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.
![](https://secure.gravatar.com/avatar/960796decb792386a8652abe32d2236c.jpg?s=120&d=mm&r=g)
At 06:17 AM 1/31/2008, you 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...
![](https://secure.gravatar.com/avatar/182974f8b2562287a54415119be4535c.jpg?s=120&d=mm&r=g)
Toby Dickenson wrote:
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