[Twisted-Python] Cascading services (passing events through connected services)

glyph at divmod.com glyph at divmod.com
Tue May 6 16:09:24 MDT 2008

On 09:38 pm, matthieu.brucher at gmail.com wrote:
>I'm currently trying to implement the JXTA standard 
>(http://www.jxta.org) in
>Python using twisted (because it seems to be the right choice). I've
>implemented the basic parts that are not network related, that is, not 

Of course it's the right choice.  Twisted is *awesome* ;-).
>network -> TCP transport layer service -> endpoint service -> some 
>high level services -> endpoint service -> TCP transport layer

You use the word "service" a lot here and I'm not sure I understand it. 
There are, unfortunately, a dozen or so things you could mean by it.
>Is there a sample somewhere that does something like this ? Each time 
>event is sent to a listener (the TCP transport layer, the endpoint, 
>that acts upon it. I've tried to find some samples over the Internet, 
>perhaps someone has a pointer to help me ?

Assuming I do understand what all those "service" and "->"s mean in the 
above text, all you need to do is to

  1) have some objects
  2) keep references between them to other objects
  3) call methods on the relevant objects when the relevant things happen

Quite often, people somehow arrive at the incorrect conclusion that 
because twisted is event-driven, it has some magic "event" mechanism you 
have to use to get different pieces of code to talk to each other. 
Quite the opposite; the whole point of Twisted is to reduce event-driven 
programming to just regular Python method calls.

It may help, if you need to relay messages from one connected Protocol 
object to another, that Protocol objects have a reference to their 
factory, and you can easily keep a list of "all connections" on that 
factory.  This is actually two of our FAQs - have a look at this one and 
the next one:


More information about the Twisted-Python mailing list