简体   繁体   中英

Twisted deferred chaining

I have an application that manages modules calls asynchronously:

  • it requests a deferred that triggers itself
  • appends custom callback
  • checks the returned code to see if = CONTINUE , otherwise handle errors

This is the code that returns a deferred to the main application:

def xxfi_connect(self, hostname):
    d = defer.Deferred()
    d.callback(Milter.ReturnCodes.CONTINUE)
    return d

To asynchronously append some code, I need to hook up my function call in the deferred function like this:

d = defer.Deferred()
d.addCallback(self.run_mods, application.L_CONNECT)
d.callback(Milter.ReturnCodes.CONTINUE)

The trouble is that every function hooked up receive an argument containing the callback parameter (application.L_CONNECT).

Is it possible to achieve this without transporting the returncode in every function call ?

Ideally, I'd like my callback function to be like this:

def run_mods(self, level):
    pass

instead of

def run_mods(self, code, level):
    pass

because the code (Milter.ReturnCodes.CONTINUE) is only needed at the end of the chain

distinguishing Successful or Erred Deferred() s is already a feature built into them.

>>> from twisted.internet import defer
>>> d = defer.Deferred()
>>> def errors(*args): raise Exception("i'm a bad function")
>>> def sad(*arg): print "this is not so good", arg
>>> def happy(*arg): print "this is good", arg
>>> d.addCallback(errors)
<Deferred at 0x106821f38>
>>> d.addCallbacks(happy, sad)
<Deferred at 0x106821f38>
>>> d.callback("hope")
this is not so good (<twisted.python.failure.Failure <type 'exceptions.Exception'>>,)

Any given "stage" in a chain of callbacks can easily know if it's following an error state, either by how it was added, as the argument to addErrback() , or the second argument to addCallbacks() , or by virtue of it's argument being an instance of Failure()

For more about deferred chaining see: https://twistedmatrix.com/documents/14.0.1/core/howto/defer.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM