, Deferred - , , , . , , , Deferred .
, yield ing a Deferred inlineCallbacks "" a Deferred - - , ( Deferred , ), , yield, None. , - " inlineCallbacks", , 20/20: -).
, Deferred , API, Deferred, Deferred . , , , . : API, Deferred, , , JSON, .addCallback(json.loads), , JSON- , .
, async , , , :
from __future__ import print_function, unicode_literals
from twisted.internet import defer
class Foo(object):
def __init__(self):
self.dfd = defer.Deferred()
def async(self):
justForThisCall = defer.Deferred()
def callbackForDFD(result):
justForThisCall.callback(result)
return result
self.dfd.addCallback(callbackForDFD)
return justForThisCall
@defer.inlineCallbacks
def func(self):
print('Started!')
result = yield self.async()
print('Stopped with result: {0}'.format(result))
if __name__ == '__main__':
foo = Foo()
print("calling func")
foo.func()
print("firing dfd")
foo.dfd.callback('no need to wait!')
print("calling func again")
foo.func()
print("done")
:
calling func
Started!
firing dfd
Stopped with result: no need to wait!
calling func again
Started!
Stopped with result: no need to wait!
done