【问题标题】:Catching connection error in Autobahn Component在高速公路组件中捕获连接错误
【发布时间】:2021-08-03 12:47:05
【问题描述】:

我正在使用 Autobahn Python 连接到 Crossbar 路由器。我希望处理交叉开关路由器在网络上不可用的情况。当使用基于类的组件时,我能够处理连接错误,但是在使用功能组件时,我无法捕获和处理这些异常。

from autobahn.asyncio.component import Component

component = Component(transports="no_existent_url:3000",
                          realm=realm, authentication=authentication)

component.on('join', onJoinHandler)
# Called on exception when connected
component.on('leave', onLeaveHandler)
# Never called
component.on('connectfailure', onFailureHandler)
# Never called
component.on('disconnect', onDisconnectHandler)

component.start()

错误出现在控制台输出中:

Error in connection Multiple exceptions: [Errno 111] Connect call failed ('no_existent_url', 3000), [Errno 99] Cannot assign requested address
Connection failed: OSError: Multiple exceptions: [Errno 111] Connect call failed ('no_existent_url', 3000), [Errno 99] Cannot assign requested address
Component failed: Exhausted all transport connect attempts

【问题讨论】:

  • 你试过using try/except吗?
  • @ExtraFishness 是的,异常不会在主运行循环中引发,因此 try/except 不起作用

标签: python python-asyncio autobahn


【解决方案1】:

Component.start() 返回一个在组件完成或退出时解析的未来 (source code in component.py)。等待这个未来并检查.exception() 方法可以让您捕获异常。

from autobahn.asyncio.component import Component

component = Component(transports="no_existent_url:3000",
                          realm=realm, authentication=authentication)

component.on('join', onJoinHandler)
# Called on exception when connected
component.on('leave', onLeaveHandler)
# Never called
component.on('connectfailure', onFailureHandler)
# Never called
component.on('disconnect', onDisconnectHandler)

future = component.start()

result = await future
if result.exception() is not None:
  # Handler exception

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多