如何在autobahn python ApplicationSession .__ init__中引发异常?

时间:2016-09-16 14:00:54

标签: python-asyncio autobahn

我正在尝试使用带有ApplicationRunner的autobahn-0.16.0,如github上的this example所示。

如果在调用join()后引发异常,则一切都按预期工作。但是,如果在__init__方法中引发异常,我会得到AttributeError: 'ApplicationRunner' object has no attribute 'log'。我想知道是否有办法使这项工作。

下面的代码是一个重现问题的例子。

#!/usr/bin/python3

import txaio

import asyncio 
from asyncio import coroutine
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner

class SessionWithException(ApplicationSession):
    def __init__(self, config):
        ApplicationSession.__init__(self, config)
#        raise Exception('Some error exception')

    def onConnect(self):
        print("Connecting...")
        self.join(self.config.realm)
        raise Exception('Good exception')

    @coroutine
    def onJoin(self, details):
        print("Joining...")
        self.disconnect()

    def onDisconnect(self):
        print("Disconnecting...")
        asyncio.get_event_loop().stop()

def main():
    txaio.start_logging(level='error')

    runner = ApplicationRunner("wss://api.poloniex.com:443", "realm1")
    runner.run(SessionWithException)

if __name__ == "__main__":
    main()

当我对raise中的__init__发表评论时,我收到此错误:

Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/autobahn/wamp/websocket.py", line 60, in onOpen
    self._session = self.factory._factory()
  File "/usr/lib/python3.4/site-packages/autobahn/asyncio/wamp.py", line 125, in create
    self.log.failure("App session could not be created! ")
AttributeError: 'ApplicationRunner' object has no attribute 'log'

1 个答案:

答案 0 :(得分:0)

我相信这是高速公路本身的一个微妙的错误。 图书馆应尽早初始化所有参赛者的属性。 请在bug tracker

上提出问题