Scenario:
Writing a test program in order to subscribe to different topics one at a time. When calling the Start function in subscribe multiple times from another .py file, see attached code
When running Subscribe.Start it does not get to the return statement without using the reactor.stop(). When using the reactor.stop() and then subscribe to a new topic an error is thrown i.e. “Reactor not restartable”
from autobahn.twisted import sleep
from autobahn.twisted.component import Component, run
from autobahn.twisted.wamp import ApplicationRunner, ApplicationSession, Application, Session
from autobahn.wamp import auth
from autobahn.wamp.serializer import JsonSerializer
from pprint import pprint
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
class SubscribeComponent(Session):
def __init__(self,
topic: str,
wait: float):
super().__init__()
self._topic = topic
self._wait = wait
self.result = None
@inlineCallbacks
def onJoin(self, details):
def on_event(*data):
print('Subscription event received(#%d)' % len(data))
pprint(data)
self.result = data
#subscription.unsubscribe()
#self.leave()
subscription = yield self.subscribe(on_event, self._topic)
print('Subscribe to:%s sub id:%d' % (self._topic, subscription.id))
yield sleep(self._wait)
res = yield subscription.unsubscribe()
self.leave()
def onDisconnect(self):
if reactor.running:
reactor.stop()
class Subscribe:
def __init__(self,
url: str,
realm: str):
self.url = url
self.realm = realm
self.runner = ApplicationRunner(url=self.url, realm=self.realm)
def start(self, topic: str, wait: float):
# Setup Component to use wamp cra authentication.
session = SubscribeComponent(topic=topic, wait=wait)
session.add_authenticator(auth.AuthAnonymous())
try:
print('Starting %s at %s, %s, %s' % ("Send", self.url, self.realm, topic))
# self.runner.run(make=session, start_reactor=False)
self.runner.run(session, start_reactor=False)
reactor.run()
return session.result
except Exception as e:
pprint('Error, Is the router up?')
pprint(e)
return False