从应用程序中触发的python runner退出

2024-10-06 12:37:23 发布

您现在位置:Python中文网/ 问答频道 /正文

{1}开始用基本的python来建立连接。我的目的是连接一次,触发一条消息,然后退出,因为这就是我正在编写的脚本所需要做的。我遇到的问题是,在应用程序启动后如何关闭它似乎并不明显,我使用the advice here访问全局reactor并调用reactor.stop(),但我想知道是否有更好的方法。看看我下面的脚本,欢迎所有评论。在

from autobahn.twisted.wamp import ApplicationRunner, ApplicationSession

class Example(ApplicationSession):
  def __init__(self, config=None):
    ApplicationSession.__init__(self, config)

  def onJoin(self, details):
    self.register(self)
    # publish something here
    from twisted.internet import reactor
    reactor.stop()

runner = ApplicationRunner(u"ws://example_address", u"example_realm")

try:
  runner.run(Example)
except Exception as e:
  print('Couldn\'t connect to WAMP router because: "{}"'.format(e))

Tags: fromimportself脚本confighereinitexample