Skip to content

Shared Connection Object

micramm edited this page Nov 21, 2013 · 10 revisions

Introduction

The shared connection object allows multiple asynchronous clients to share a single connection to the manager. The clients work in their own unique contexts, maintaining complete independence. Additionally, the connection object helps the clients keep track of when a given server disconnects or reconnects to the manager. When such an event occurs, the client can use the connection object to automatically run a certain action like reacquiring the latest data from the server or disabling itself.

How to use

First, the asynchronous client has to important the connection object, and then connect i.e:

from connect import connection

@inlineCallbacks
def initialize(self):
    self.cxn = connection()
    yield self.cxn.connect()

Then it is necessary to acquire a unique connect context.

self.context = yield self.cxn.context()

Then can get access to any available server, and use that server in the acquired context:

server = yield self.cxn.get_server('Server Name')
yield server.method(..., context = self.context)

One can easily use the connection object to specify any action to be taken when given server connects or disconnects.

def on_connect(self):
    print 'connected'

def on_disconnect(self):
    print 'disconnected'

self.cxn.add_on_connect('Server Name', self.on_connect)
self.cxn.add_on_disconnected('Server Name', self.on_disconnect)