开发者

python twisted DeferedList ErrorBack ( Unhandled error in Deferred )

In my below code I pass a host:port combination and try to get some information from the server using twisted Defered. I have shown a very basic code of what I am trying to do. The connection to host port is made via httplib. It works correctly if the host is up. Correct callback methods are called. But when it fails ( when the retrieve url fails ), it does not go in t the printError function. I get 'Unhandled error in Deferred' error and the looping stops. Can someone please show me how I can get rid of the err开发者_JAVA百科or. Please provide a solution.

import httplib, time, sys

from twisted.internet import reactor, defer, task

from twisted.python import log

class Getter:

    def gotResults(self, x):

        ( host, port ) = x.split(":")
        conn = httplib.HTTPConnection( host, port )

        try :
            conn.request ( 'GET', '/get/data/' )
            response = conn.getresponse()
            self.d.callback ( response )
        except ( httplib.HTTPException ) :
            self.d.errback(ValueError("Error Connecting"))



    def getDummyData(self, x):
        currTime = time.strftime( "%H:%M:%S" )
        print currTime
        self.d = defer.Deferred()
        self.gotResults(x)
        return self.d



def printData(data):

    for d in data:
        print "Results %s %s" % ( str(d[1].status), str(d[1].reason)  )

def printError(data):

    print data



def testmessage():
# this series of callbacks and errbacks will print an error message
    g = Getter()
    deferred1 = g.getDummyData( 'valid_hostname1:port1' )
    # this should go to printData

    g = Getter()
    deferred2 = g.getDummyData('invalid_hostname2:port2')
    # this should go to printError

    d1 = defer.DeferredList ( [ deferred1, deferred2 ] )
    d1.addCallback ( printData )
    d1.addErrback  ( printError )



x = task.LoopingCall ( testmessage )
x.start ( 1 )
reactor.callLater(300, reactor.stop);
reactor.run()


The problem is that you have assumed that conn.request (on your line 17) will raise a httplib.HTTPException, but that's not the exception type it raises. Since you're not terribly sure what exception it will raise, I'd recommend you make a blanket except... statement and fetch the exception data from sys.exc_info().

Fix it along the lines of:

import httplib, time, sys

from twisted.internet import reactor, defer, task

from twisted.python import log

class Getter:

    def gotResults(self, x):

        ( host, port ) = x.split(":")
        conn = httplib.HTTPConnection( host, port )

        try :
            conn.request ( 'GET', '/get/data/' )
            response = conn.getresponse()
            self.d.callback ( response )
        except:
            self.d.errback(sys.exc_info())


    def getDummyData(self, x):
        currTime = time.strftime( "%H:%M:%S" )
        print currTime
        self.d = defer.Deferred()
        self.gotResults(x)
        return self.d



def printData(data):

    for d in data:
        print "Results %s %s" % ( str(d[1].status), str(d[1].reason)  )

def printError(data):

    print data



def testmessage():
# this series of callbacks and errbacks will print an error message
    g = Getter()
    deferred1 = g.getDummyData( 'valid_hostname1:port1' )
    # this should go to printData

    g = Getter()
    deferred2 = g.getDummyData('invalid_hostname2:port2')
    # this should go to printError

    d1 = defer.DeferredList ( [ deferred1, deferred2 ] )
    d1.addCallback ( printData )
    d1.addErrback  ( printError )



x = task.LoopingCall ( testmessage )
x.start ( 1 )
reactor.callLater(300, reactor.stop);
reactor.run()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜