Clean shutdown of Autobahn Python WAMP client

#1

Hi,

I found it quite hard to figure out how to shutdown an Autobahn Python WAMP client cleanly on ctrl-c. Below I pasted the code that eventually worked for me (Autobahn Python 0.10.9, Twisted 15.4.0, Crossbar 0.11.1).

Is there a simpler way to do this?

What I found difficult:

  • In order to leave the session on ctrl-c I needed access to the leave method of the ApplicationSession instance.

  • For this I had to create an Application class instance and pass its call method to the constructor of the WAMP service. call stores the session in the Application instance.

  • This was not obvious to me from the Autobahn examples I knew, since they use the ApplicationSession class.

  • I used Twisted services, so that ctrl-c initiates the shutdown steps.

I imagine that a clean shutdown on ctrl-c is desirable for many users. It would be good to have an example of this in the repository.

Thanks

Florian

from autobahn.twisted import wamp

from autobahn.twisted.wamp import Application

from autobahn.twisted.wamp import ApplicationSession

from twisted.application import service

from twisted.internet import reactor

from twisted.internet.defer import inlineCallbacks

class MyApplication(Application):

def __init__(self):

	Application.__init__(self)

	ip = '127.0.0.1'

	port = '8080'

	self.uri = "ws://" + ip + ":" + port + "/ws"

	self.realm = "realm1"

	self._signals.setdefault("onjoined", []).append(self.onJoin)

	self._signals.setdefault("onleave", []).append(self.onDisconnect)

def leaveSession(self):

	if not self.session:

		return

	return self.session.leave()

def onJoin(self):

	print("Joined.")

def onDisconnect(self):

	print("Disconnecting.")

class MyWampService(wamp.Service):

def __init__(self, app):

	self.app = app

	wamp.Service.__init__(self, self.app.uri, self.app.realm, self.app.__call__, debug=False, debug_wamp=False)

def startService(self):

	print("Starting my service.")

	wamp.Service.startService(self)

@inlineCallbacks

def stopService(self):

	print("Stopping my service.")

	yield self.app.leaveSession()

	yield wamp.Service.stopService(self)

application = service.Application(“ctrlc”)

myApplication = MyApplication()

myWampService = MyWampService(myApplication)

myWampService.setServiceParent(application)

twistd --nodaemon --python ctrlc.py

2015-11-17 15:34:40-0500 [-] Log opened.

2015-11-17 15:34:40-0500 [-] twistd 15.4.0 starting up.

2015-11-17 15:34:40-0500 [-] reactor class: twisted.internet.epollreactor.EPollReactor.

2015-11-17 15:34:40-0500 [-] Starting my service.

2015-11-17 15:34:40-0500 [-] Starting factory <autobahn.twisted.websocket.WampWebSocketClientFactory object at 0x7f605e816a50>

2015-11-17 15:34:40-0500 [-] Joined.

^C2015-11-17 15:34:42-0500 [-] Received SIGINT, shutting down.

2015-11-17 15:34:42-0500 [-] Stopping my service.

2015-11-17 15:34:42-0500 [-] Disconnecting.

2015-11-17 15:34:42-0500 [-] Connection to/from tcp4:127.0.0.1:8080 was closed cleanly

2015-11-17 15:34:42-0500 [-] Stopping factory <autobahn.twisted.websocket.WampWebSocketClientFactory object at 0x7f605e816a50>

2015-11-17 15:34:42-0500 [-] Main loop terminated.

2015-11-17 15:34:42-0500 [-] Server Shut Down.

0 Likes

#2

Hi,

I was coming from an earlier version (0.9.5) and had not realized that the code in version 0.10.9 already has methods cleanup and init_proto in ApplicationRunner that deal with a clean shutdown on ctrl-c. Given this the minimal example using ApplicationRunner shuts down cleanly on ctrl-c. The more complicated stuff I pasted is not needed for that.

Thanks

Florian

···

On Tuesday, November 17, 2015 at 4:31:31 PM UTC-5, Florian Conrady wrote:

Hi,

I found it quite hard to figure out how to shutdown an Autobahn Python WAMP client cleanly on ctrl-c. Below I pasted the code that eventually worked for me (Autobahn Python 0.10.9, Twisted 15.4.0, Crossbar 0.11.1).

Is there a simpler way to do this?

What I found difficult:

  • In order to leave the session on ctrl-c I needed access to the leave method of the ApplicationSession instance.
  • For this I had to create an Application class instance and pass its call method to the constructor of the WAMP service. call stores the session in the Application instance.
  • This was not obvious to me from the Autobahn examples I knew, since they use the ApplicationSession class.
  • I used Twisted services, so that ctrl-c initiates the shutdown steps.

I imagine that a clean shutdown on ctrl-c is desirable for many users. It would be good to have an example of this in the repository.

Thanks

Florian

from autobahn.twisted import wamp

from autobahn.twisted.wamp import Application

from autobahn.twisted.wamp import ApplicationSession

from twisted.application import service

from twisted.internet import reactor

from twisted.internet.defer import inlineCallbacks

class MyApplication(Application):

def init(self):

  Application.__init__(self)
  ip = '127.0.0.1'
  port = '8080'
  self.uri = "ws://" + ip + ":" + port + "/ws"
  self.realm = "realm1"
  self._signals.setdefault("onjoined", []).append(self.onJoin)
  self._signals.setdefault("onleave", []).append(self.onDisconnect)

def leaveSession(self):

  if not self.session:
  	return
  return self.session.leave()

def onJoin(self):

  print("Joined.")

def onDisconnect(self):

  print("Disconnecting.")

class MyWampService(wamp.Service):

def init(self, app):

  self.app = app
  wamp.Service.__init__(self, self.app.uri, self.app.realm, self.app.__call__, debug=False, debug_wamp=False)

def startService(self):

  print("Starting my service.")
  wamp.Service.startService(self)

@inlineCallbacks

def stopService(self):

  print("Stopping my service.")
  yield self.app.leaveSession()
  yield wamp.Service.stopService(self)

application = service.Application(“ctrlc”)

myApplication = MyApplication()

myWampService = MyWampService(myApplication)

myWampService.setServiceParent(application)

twistd --nodaemon --python ctrlc.py

2015-11-17 15:34:40-0500 [-] Log opened.

2015-11-17 15:34:40-0500 [-] twistd 15.4.0 starting up.

2015-11-17 15:34:40-0500 [-] reactor class: twisted.internet.epollreactor.EPollReactor.

2015-11-17 15:34:40-0500 [-] Starting my service.

2015-11-17 15:34:40-0500 [-] Starting factory <autobahn.twisted.websocket.WampWebSocketClientFactory object at 0x7f605e816a50>

2015-11-17 15:34:40-0500 [-] Joined.

^C2015-11-17 15:34:42-0500 [-] Received SIGINT, shutting down.

2015-11-17 15:34:42-0500 [-] Stopping my service.

2015-11-17 15:34:42-0500 [-] Disconnecting.

2015-11-17 15:34:42-0500 [-] Connection to/from tcp4:127.0.0.1:8080 was closed cleanly

2015-11-17 15:34:42-0500 [-] Stopping factory <autobahn.twisted.websocket.WampWebSocketClientFactory object at 0x7f605e816a50>

2015-11-17 15:34:42-0500 [-] Main loop terminated.

2015-11-17 15:34:42-0500 [-] Server Shut Down.

0 Likes