def syncRunServer(srv, host=C.MANAGER_HOST, port=C.MANAGER_PORT, password=None): """Run a labrad server of the specified class in a synchronous context. Returns a context manager to be used with python's with statement that will yield when the server has started and then shut the server down after the context is exited. """ if password is None: password = C.PASSWORD srv.password = password @inlineCallbacks def start_server(): connector = reactor.connectTCP(host, port, srv) yield srv.onStartup() returnValue(connector) @inlineCallbacks def stop_server(): yield srv.onShutdown() thread.startReactor() connector = blockingCallFromThread(reactor, start_server) try: yield finally: try: connector.disconnect() blockingCallFromThread(reactor, stop_server) except Exception: pass # don't care about exceptions here
def syncRunServer(srv, host=C.MANAGER_HOST, port=None, username=None, password=None, tls_mode=C.MANAGER_TLS): """Run a labrad server of the specified class in a synchronous context. Returns a context manager to be used with python's with statement that will yield when the server has started and then shut the server down after the context is exited. """ from labrad import protocol tls_mode = C.check_tls_mode(tls_mode) if port is None: port = C.MANAGER_PORT_TLS if tls_mode == 'on' else C.MANAGER_PORT @inlineCallbacks def start_server(): p = yield protocol.connect(host, port, tls_mode, username, password) yield srv.startup(p) @inlineCallbacks def stop_server(): srv.disconnect() yield srv.onShutdown() thread.startReactor() blockingCallFromThread(reactor, start_server) try: yield finally: try: blockingCallFromThread(reactor, stop_server) except Exception: pass # don't care about exceptions here
def recalibrate(boardname, carrierMin, carrierMax, zeroCarrierStep=0.025, sidebandCarrierStep=0.05, sidebandMax=0.35, sidebandStep=0.05, corrector=None): startReactor() block(recalibrateAsync, boardname, carrierMin, carrierMax, zeroCarrierStep, sidebandCarrierStep, sidebandMax, sidebandStep, corrector)
def DACcorrector(fpganame, channel, cxn=None, lowpass=gaussfilter, bandwidth=0.13): if cxn != None: print 'Warning: cxn argument is obsolete and is not being used.' print 'It is only there for backwards compatibility' startReactor() return block(DACcorrectorAsync, fpganame, channel, None, lowpass, bandwidth)
def connect(self, host, port=C.MANAGER_PORT, timeout=C.TIMEOUT, password=None): thread.startReactor() self._cxn = block(getConnection, host, port, self.name, password) self._mgr = ILabradManager(self._cxn) self.ID = self._cxn.ID self.host = host self.port = port self.connected = True
def connect(host=C.MANAGER_HOST, port=None, name=None, timeout=C.TIMEOUT, **kw): """Create a backend connection to labrad. This connects to labrad asynchronously and then wraps the underlying async connection object in a synchronous TwistedConnection interface. """ name = name or 'Python Client ({})'.format(support.getNodeName()) thread.startReactor() future = concurrent.call_future(getConnection, host, port, name, **kw) cxn = future.result(timeout=timeout) return TwistedConnection(cxn)
def IQcorrector(fpganame, cxn=None, zerocor=True, pulsecor=True, iqcor=True, lowpass=cosinefilter, bandwidth=0.4): if cxn != None: print 'Warning: cxn argument is obsolete and is not being used.' print 'It is only there for backwards compatibility.' startReactor() corrector = block(IQcorrectorAsync, fpganame, None, zerocor, pulsecor, iqcor, lowpass, bandwidth) corrector.recalibrationRoutine = recalibrate return corrector
def syncRunServer(srv, host=C.MANAGER_HOST, port=None, password=None, tls=C.MANAGER_TLS): """Run a labrad server of the specified class in a synchronous context. Returns a context manager to be used with python's with statement that will yield when the server has started and then shut the server down after the context is exited. """ tls = C.check_tls_mode(tls) if port is None: port = C.MANAGER_PORT_TLS if tls == 'on' else C.MANAGER_PORT if password is None: password = C.PASSWORD srv.password = password @inlineCallbacks def start_server(): srv.configure_tls(host, tls) if tls == 'on': tls_options = crypto.tls_options(host) reactor.connectSSL(host, port, srv, tls_options) else: reactor.connectTCP(host, port, srv) yield srv.onStartup() @inlineCallbacks def stop_server(): srv.disconnect() yield srv.onShutdown() thread.startReactor() blockingCallFromThread(reactor, start_server) try: yield finally: try: blockingCallFromThread(reactor, stop_server) except Exception: pass # don't care about exceptions here
def _connect(self, password, _timeout, tls_mode): @defer.inlineCallbacks def _connect_deferred(): cxn = yield getConnection(self.host, self.port, self.name, password, tls_mode=tls_mode) self._connected.set() # Setup a coroutine that will clear the connected flag when the # connection is lost. We launch this but do not yield to wait # for the result because we want this to happen asynchronously # in the background. @defer.inlineCallbacks def handle_disconnect(): try: yield cxn.onDisconnect() except Exception: pass self._connected.clear() handle_disconnect() defer.returnValue(cxn) startReactor() self.cxn = self.call(_connect_deferred).result() return self.cxn.ID
def setup_class(cls): # make sure twisted reactor is running thread.startReactor()
def _connect(self, password, _timeout): startReactor() self.cxn = self.call(getConnection, self.host, self.port, self.name, password).wait() return self.cxn.ID
def run(pipe, inputs, *a, **kw): """Run inputs through a pipeline.""" pl = Pipeline(pipe) return pl.run(inputs, *a, **kw) if __name__ == '__main__': from datetime import datetime #from labrad.util import timing def delayedWakeup(t, data=None): return Future(util.wakeupCall, t, data) startReactor() def pipe1(i): for k in range(10): r = yield Priority(10-k), delayedWakeup(0.1 * k, k) returnValue(i) letters = [chr(65+n) for n in range(10)] pl = PipelineBase(pipe1) #t1 = timing(pl.run, n=1, inputs=range(10)) pl = Pipeline(pipe1) #t2 = timing(pl.run, n=1, inputs=range(10)) print 'pipe 1.'
def reactor_thread(environ_setup): thread.startReactor() yield None thread.stopReactor()
"""Run inputs through a pipeline.""" pl = Pipeline(pipe) return pl.run(inputs, *a, **kw) if __name__ == '__main__': from datetime import datetime #from labrad.util import timing def delayedWakeup(t, data=None): return Future(util.wakeupCall, t, data) startReactor() def pipe1(i): for k in range(10): r = yield Priority(10 - k), delayedWakeup(0.1 * k, k) returnValue(i) letters = [chr(65 + n) for n in range(10)] pl = PipelineBase(pipe1) #t1 = timing(pl.run, n=1, inputs=range(10)) pl = Pipeline(pipe1) #t2 = timing(pl.run, n=1, inputs=range(10)) print 'pipe 1.'