[Twisted-Python] way of dealing with returning a deferred from a method/function

Itamar Shtull-Trauring itamar at itamarst.org
Wed Dec 3 21:37:38 EST 2003


On Wed, 03 Dec 2003 20:48:56 -0500
Jonathan Simms <slyphon at twistedmatrix.com> wrote:

> I was thinking about it like this (hence my confusion):
> 
> def aLongWait(stuff):		# obviously incorrect
>     d = defer.Deferred()   
>     return d
>     result = doSomeStuffHere(stuff)
>     d.callback(result)
> 
> 
> the little trick I figured out today is as follows:
> 
> def aLongWait(stuff):
>     d = defer.Deferred()
>     def _():
>          result = doSomeStuffHere(stuff)
>          d.callback(result)
>     reactor.callLater(0, _)
>     return d

Why is this any different? all it does is make d get its result slightly
afterwards. It doesn't change anything fundemantal. It's virtually
identical to:

def aLongWait(stuff):
    result = doSomeStuffHere(stuff)
    d = defer.Deferred()
    d.callback(result)
    return d

-- 
Itamar Shtull-Trauring    http://itamarst.org/
Available for Python & Twisted consulting




More information about the Twisted-Python mailing list