[Twisted-Python] Learning Twisted

Glyph Lefkowitz glyph at divmod.com
Sat May 15 16:09:04 MDT 2004


Jason Diamond wrote:
> Sorry for the length of this post but I learn best by trying to
> explain what I'm learning (even if nobody's listening). Twisted seems
> very cool but also *huge* and unlike any framework I've used before so
> I thought I'd post my first experiences with it in the hopes that I
> could be corrected where needed (and maybe even help people other new
> like me).

Thank you for sending this post.  I wish every new user would give such 
great feedback!

> So how'd I do for a Twisted newbie? Am I on the path to enlightenment?
> Are there any obvious errors in what I've interpreted so far? Is this
> a "good" implementation for this feature or is their a more
> Twisted-ish approach I need to strive for?

Although I'm not an expert with the IMAP code, everything seemed right 
except this last part:

> f = MyIMAP4ClientFactory()
> reactor.connectTCP("server", 143, f)
> reactor.run()

If possible, you should try to run Twisted code under twistd, which 
means that you'd remove that code from your Python module, and put it 
into a separate .tac file that read something like:

from twisted.application.internet import TCPClient
from twisted.application.service import Application, IServiceCollection
application = Application("my client application")
f = MyIMAP4ClientFactory()
client = TCPClient("server", 143, f)
client.setServiceParent(application)

Even if you do have a need to put your code into its own script - and 
the twistd support for clients is admittedly weaker than that for 
servers - it's generally a good idea not to put it in the same script as 
  your code.

> Am I correct in assuming that the IMAP4Client documentation needs some
> buffing up? (Maybe I can help with that.) I didn't look at any source
> code (other than in the HOWTOs) in implementing this so I think that's
> a good sign. But as I mentioned above, I'm not clear on how I know
> what the signature for my callbacks should be or what the parameters
> to those callbacks mean in some cases.

Yes.  Pretty much every aspect of the documentation needs help.  It 
would be greatly appreciated!

> Also, it's not obvious to me how protocol implementors decide when to
> have users override a method versus having them use callbacks. I know
> I'm working on client code but knowing this might help me know how to
> use their code. Is it a matter of personal preference? The ircLogBot
> example didn't have any callbacks using deferreds but it looks like I
> had no choice when using IMAP4Client.

It's not purely preference, but there is some overlap.  Generally, what 
Deferreds are used for are when you've got some operation that happens 
once, with one result: you want to delete a message, for example, and 
get a notification when that deletion operation has completed.  Methods 
on objects are used when you are doing something more persistent, for 
example, connecting a client to a server - there is a potential for 
success or failure of that one operation, true, but the more interesting 
thing is the continuous stream of data being received from or sent to 
the other side of the connection.




More information about the Twisted-Python mailing list