Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何捕获源自线程的延迟中未处理的错误。deferToThread()_Python_Python 3.x_Python 2.7_Twisted - Fatal编程技术网

Python 如何捕获源自线程的延迟中未处理的错误。deferToThread()

Python 如何捕获源自线程的延迟中未处理的错误。deferToThread(),python,python-3.x,python-2.7,twisted,Python,Python 3.x,Python 2.7,Twisted,我在延迟中获取未处理的错误: 谁能帮忙,怎么处理 @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 Av

我在延迟中获取未处理的错误: 谁能帮忙,怎么处理

    @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))
错误在线路上抛出

key_data = yield threads.deferToThread(self.discovery.find_key, self.userdata)
根据,您可以使用try/except语句处理此情况:

例如:

@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))
因此,将您的
key\u data=yield…
行替换为:

try:
    key_data = yield threads.deferToThread(self.discovery.find_key, self.userdata)
except SomeExceptionYouCanHandle:
    # Some exception handling code
根据,您可以使用try/except语句处理此情况:

例如:

@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))
因此,将您的
key\u data=yield…
行替换为:

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

这对于大多数使用
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)
另一种方法是使用
addCallback
(成功)和
addErrback
(失败)链接回调。因此,您应该能够这样做:

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
有用的链接
对于大多数使用
内联回调的开发人员来说,这是一种有意义的方式

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)
另一种方法是使用
addCallback
(成功)和
addErrback
(失败)链接回调。因此,您应该能够这样做:

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
有用的链接

功能可能引发异常(延迟可能引发故障)。你的问题是什么?你好@Jean PaulCalderone我收到一个错误“未处理的延迟错误:”我的程序崩溃了。我希望通过记录一条消息(如“未找到key_数据”)而不是“deffered中未处理的错误”来优雅地处理它。函数可能引发异常(延迟可能引发故障)。你的问题是什么?你好@Jean PaulCalderone我收到一个错误“未处理的延迟错误:”我的程序崩溃了。我想通过记录一条类似于(“未找到密钥数据”)的消息来优雅地处理它,而不是“deffered中未处理的错误”。非常感谢@austage.no,这很有帮助:)chaining addcallback方法对我有效。然而,try..except方法抛出了这个“keysign.discover(DEBUG):无法在Deferred中获得未经处理的key_数据错误:“Thank you some@sandogram.no,它有帮助:)chain addcallback方法对我有效。但是,try..except方法抛出以下“keysign.discover(DEBUG):无法获取延迟中未处理的密钥数据错误:”