john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

deferred callback errback callbacks addboth

from twisted.internet.defer import Deferred, AlreadyCalledError

def got_text( result ):
    print 'SUCCESS Your text:', result

def got_text_failed( err ):
    print '\nError: could not get text because'
    print err
    # reactor.stop()

d = Deferred()
d.addCallbacks( got_text, got_text_failed ) # add a callback/errback functions to the chain
d.callback( 'some text here' )      # fire the chain with a normal result


try:
    d.callback( 'some text here' )
except AlreadyCalledError as error:
    print "AlreadyCalledError shows how callbacks can only be called once"


e = Deferred()
e.addCallback( got_text )           # registers a "pass through" for the same level errBack chain
e.addErrback( got_text_failed )     # registers a "pass through" for the same level callBack chain

e.errback( )        # fire the chain with an error result, similar to raise Exception

print "Finished"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# https://github.com/hipchat/pusher

    @app.route("/john", methods=["GET"])
    def john(self, request):
        def logdata(d):
            log.warn(d)

        device_token = '99ed7cbcb32764bff208fd66ba90b81ab252ce791iosdevicetoken'  # John iOS 5
        aps = {'alert': 'John Says', 'badge': 42}
        payload = {'aps': aps}
        my_pusher = self.mgr.core.pusher_service
        result = my_pusher.apns.send_push(device_token, payload)
        log.warn(repr(result))
        result.addCallback(logdata)  # pass a function that does something with the data, expects True
        return "John says hi"

  • « apple push notification client
  • For loop from file infinite while subdirs cut delimit strtok name diff du sh »

Published

Mar 28, 2014

Category

python-twisted

~153 words

Tags

  • addboth 1
  • callback 1
  • callbacks 2
  • deferred 1
  • errback 2
  • python 180
  • twisted 20