Skip to content

Writing LabRAD Servers (Basic)

micramm edited this page Nov 27, 2012 · 1 revision

Optional Methods

initServer

stopServer

The stopServer method is meant for cleanup actions that have to be called when the server is required to stop. This method is executed, for example, when the node server stops running the given server, or whenever the server exits with an error such as the identification error. #needlink.

Since it's not known ahead of time when stopServer may get called, it's important account for any possible state of the server. Suppose some big object is created in initServer and requires to be cleaned up upon exiting:

def initServer():
   self.my_object = big_object()

One should not simply use self.my_object.cleanup() in stopServer because stopServer may get called even before initServer is executed. This would happen with the identification error #needlink. Instead use:

def stopServer():
    if hasattr(self, 'my_object'):
        self.my_object.cleanup()

Perhaps more pythonic is:

def stopServer():
    try:
        self.my_object.cleanup():
    except AttributeError:
        pass

Note that stopServer may return a deferred, which means that one can yield inside the method. with additional @inlineCallbacks decorator. #needlink

initContext