How to catch unhandled error in Deferred originating from threads.deferToThread()

Deadly 提交于 2019-12-11 07:24:06

问题


I'm getting the error Unhandled error in Deferred: Can anybody help, how to handle this?

    @inlineCallbacks
    def start(self):
        # First we try Avahi, if it fails we fallback to Bluetooth because
        # the receiver may be able to use only one of them
        log.info("Trying to use this code with Avahi: %s", self.userdata)
        key_data = yield threads.deferToThread(self.discovery.find_key, self.userdata)
        if key_data and not self.stopped:
            success = True
            message = ""
            returnValue((key_data, success, message))
        if self.bt_code and not self.stopped:
            # We try Bluetooth, if we have it
            log.info("Trying to connect to %s with Bluetooth", self.bt_code)
            self.bt = BluetoothReceive(self.bt_port)
            msg_tuple = yield self.bt.find_key(self.bt_code, self.mac)
            key_data, success, message = msg_tuple
            if key_data:
                # If we found the key
            returnValue((key_data, success, message))

Error throws at line

key_data = yield threads.deferToThread(self.discovery.find_key, self.userdata)

回答1:


This is the way that makes sense for most devs using inlineCallbacks

try:
    key_data = yield threads.deferToThread(self.discovery.find_key, self.userdata)
except Exception as e:
    log.exception('Unable to get key_data')
    returnValue(e)

Another way would be to chain callback using addCallback (success) and addErrback (failure). So you should be able to do something like this:

d = threads.deferToThread(self.discovery.find_key, self.userdata)    # notice there's no yield
d.addCallback(success_callback)
d.addErrback(failure_callback)
key_data = yield d

Helpful Links

http://twistedmatrix.com/documents/current/core/howto/defer-intro.html#simple-failure-handling http://twistedmatrix.com/documents/current/core/howto/threading.html




回答2:


Per the inlineCallbacks documentation you can handle this case with a try/except statement:

For example:

@inlineCallbacks
def getUsers(self):
    try:
        responseBody = yield makeRequest("GET", "/users")
    except ConnectionError:
       log.failure("makeRequest failed due to connection error")
       returnValue([])

    returnValue(json.loads(responseBody))

Therefore, replace your key_data = yield ... line with:

try:
    key_data = yield threads.deferToThread(self.discovery.find_key, self.userdata)
except SomeExceptionYouCanHandle:
    # Some exception handling code


来源:https://stackoverflow.com/questions/49491568/how-to-catch-unhandled-error-in-deferred-originating-from-threads-defertothread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!