[Twisted-Python] Deferred ownership and the Protocols

Jean-Paul Calderone exarkun at divmod.com
Tue Jan 31 09:32:29 EST 2006


On Fri, 27 Jan 2006 20:27:20 -0800 (PST), Andy Leszczynski <leszczynscy at yahoo.com> wrote:
>Hi,
>
>I am in the process of designing the custom Protocol
>and will use Deferred-s in order to pass the
>asynchronous results. I try to learn from the current
>Twisted code base.
>
>For instance there are two modules smtp.py and
>web/client.py:
>
>def getPage(url, contextFactory=None, *args,
>**kwargs):
>[
]
>    factory = HTTPClientFactory(url, *args, **kwargs)
>[
]
>        reactor.connectTCP(host, port, factory)
>[
]
>    return factory.deferred
>
>
>
>
>def sendEmail(smtphost, fromEmail, toEmail, content,
>headers = None, attachments = None, multipartbody =
>"mixed"):
>[
]
>    d = defer.Deferred()
>    factory = SMTPSenderFactory(fromEmail, toEmail, f,
>d)
>    reactor.connectTCP(smtphost, 25, factory)
>    return d
>
>
>In the first case the deferred is owned/created by the
>Factory, in the second by the sendEmail helper. Is
>there any reason behind those, what is the
>recommendation? If my question is not clear :-), let
>me know.

It doesn't really make a difference.  I think I slightly prefer the former, but even in the latter case, sendMail and SMTPSenderFactory are basically agents of the same intent.

Jean-Paul




More information about the Twisted-Python mailing list