[Twisted-Python] Learning Twisted

Jason Diamond jason at injektilo.org
Sun May 16 19:32:51 MDT 2004


Christopher Armstrong wrote:

> Glyph Lefkowitz wrote:
>
>> Jason Diamond wrote:
>>
>>> 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.
>
>
> Also, the "request" vs "arbitrary events" distinction should be 
> clarified: Deferreds (i.e., callbacks) are used when you want to 
> expose an API that allows a request to be made for some operation to 
> occur, and they'll get triggered when the operation is complete (or 
> has errored). Methods to be overridden are there for when there's no 
> 1:1 mapping of requests to responses, when these events can happen 
> even without expecting them.

Ah! This makes perfect sense to me. So I can think of overridable 
methods as events that didn't require any action on my part to fire.

However, the ircLogBot example [1], implies that calling self.join will 
eventually invoke self.joined as if self.joined was the permanent 
callback for calling self.join. I like your explanation for the 
distinction between deferreds and methods but this seems to be blurring 
the two. What happens if an error occurred while trying to join a 
channel? (I'm not trying to pick anybody--I'm just trying to make sense 
of the examples that I'm seeing.)

-- Jason

[1]
http://twistedmatrix.com/documents/current/howto/clients#auto5





More information about the Twisted-Python mailing list