[Twisted-Python] A Python metaclass for Twisted allowing __init__ to return a Deferred
Esteve Fernandez
esteve at sindominio.net
Mon Nov 3 07:03:42 MST 2008
On Monday 03 November 2008 14:53:24 Esteve Fernandez wrote:
> On Monday 03 November 2008 14:11:05 Terry Jones wrote:
> > >>>>> "Esteve" == Esteve Fernandez <esteve at sindominio.net> writes:
> >
> > Esteve> I'm going to jump in. How about this:
> >
> > That's nice. But you've coupled the args to aFuncReturningADeferred and
> > what it's Deferred ends up returning to the args for __init__ of the
> > class.
>
> Yep, that's what I meant. I wanted to show how to pass an argument to Foo,
> which will be used to call a function (that may take a considerable amount
> of time) and whose return value will be stored in a Foo instance.
Alternatively a function would suffice:
from twisted.internet import defer, reactor
def aFuncReturningADeferred(value):
d = defer.Deferred()
reactor.callLater(5, d.callback, value[::-1])
return d
class SimpleFoo(object):
def __init__(self, value):
self.value = value
def printFoo(obj):
print obj.value
reactor.stop()
def FooFactory(value):
return aFuncReturningADeferred(value).addCallback(SimpleFoo)
d = FooFactory("Some value")
d.addCallback(printFoo)
reactor.run()
More information about the Twisted-Python
mailing list