def ensure_connected_to_zlog(): """Ensure we are connected to a zlog server. If one is not running and we are the top-level process, start a zlog server configured according to LabConfig.""" global _connected_to_zlog if _connected_to_zlog: return # setup connection with the zlog server: client = ProcessTree.instance().zlog_client if gethostbyname(client.host) == gethostbyname('localhost'): try: # short connection timeout if localhost, don't want to # waste time: client.ping(timeout=0.05) except zmq.ZMQError: # No zlog server running on localhost. Start one. It will run forever, even # after this program exits. This is important for other programs which might # be using it. I don't really consider this bad practice since the server is # typically supposed to be running all the time: zprocess.start_daemon( [sys.executable, '-m', 'labscript_utils.zlog', '--daemon'] ) # Try again. Longer timeout this time, give it time to start up: client.ping(timeout=15) else: client.ping() _connected_to_zlog = True
def figure_to_clipboard(figure=None, **kwargs): """Copy a matplotlib figure to the clipboard as a png. If figure is None, the current figure will be copied. Copying the figure is implemented by calling figure.savefig() and then copying the image data from the resulting file. Any keyword arguments will be passed to the call to savefig(). If bbox_inches keyword arg is not provided, bbox_inches='tight' will be used""" import matplotlib.pyplot as plt from zprocess import start_daemon import tempfile if not 'bbox_inches' in kwargs: kwargs['bbox_inches'] = 'tight' if figure is None: figure = plt.gcf() with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as f: tempfile_name = f.name figure.savefig(tempfile_name, **kwargs) tempfile2clipboard = os.path.join(LYSE_DIR, 'tempfile2clipboard.py') start_daemon( [sys.executable, tempfile2clipboard, '--delete', tempfile_name])
def connect_to_zlock_server(): # Ensure we are connected to a zlock server, and start one if one is supposed # to be running on localhost but is not. client = ProcessTree.instance().zlock_client if gethostbyname(client.host) == gethostbyname('localhost'): try: # short connection timeout if localhost, don't want to # waste time: client.ping(timeout=0.05) except zmq.ZMQError: # No zlock server running on localhost. Start one. It will run forever, even # after this program exits. This is important for other programs which might # be using it. I don't really consider this bad practice since the server is # typically supposed to be running all the time: zprocess.start_daemon( [sys.executable, '-m', 'labscript_utils.zlock', '--daemon'] ) # Try again. Longer timeout this time, give it time to start up: client.ping(timeout=15) else: client.ping() # Check if the zlock server supports read-write locks: global _zlock_server_supports_readwrite if hasattr(client, 'get_protocol_version'): version = client.get_protocol_version() if LooseVersion(version) >= LooseVersion('1.1.0'): _zlock_server_supports_readwrite = True # The user can call these functions to change the timeouts later if they # are not to their liking: client.set_default_timeout(ZLOCK_DEFAULT_TIMEOUT)
def connect_to_zlock_server(): # setup connection with the zprocess.locking server, depending on labconfig settings: config = LabConfig(required_params={ 'ports': ['zlock'], 'servers': ['zlock'] }) host = config.get('servers', 'zlock') port = config.get('ports', 'zlock') if socket.gethostbyname(host) == socket.gethostbyname('localhost'): try: # short connection timeout if localhost, don't want to # waste time: zprocess.locking.connect(host, port, timeout=0.05) except zmq.ZMQError: # No zprocess.locking server running on localhost. Start one. It will run # forever, even after this program exits. This is important for # other programs which might be using it. I don't really consider # this bad practice since the server is typically supposed to # be running all the time: start_daemon([sys.executable, '-m', 'zprocess.locking']) # Try again. Longer timeout this time, give it time to start up: zprocess.locking.connect(host, port, timeout=15) else: zprocess.locking.connect(host, port) # Check if the zlock server supports read-write locks: global _server_supports_readwrite if hasattr(zprocess.locking, 'get_protocol_version'): version = zprocess.locking.get_protocol_version() if LooseVersion(version) >= LooseVersion('1.1.0'): _server_supports_readwrite = True # The user can call these functions to change the timeouts later if they # are not to their liking: set_default_timeout(DEFAULT_TIMEOUT)
def ensure_connected_to_zlog(maxBytes, backupCount): """Ensure we are connected to a zlog server. If one is not running, start one with the given maxBytes and backupCount.""" global _connected_to_zlog if _connected_to_zlog: return # setup connection with the zlog server on localhost try: # short connection timeout on localhost, don't want to waste time: zprocess.zlog.connect(timeout=0.05) except zmq.ZMQError: # No zlog server running on localhost. Start one. It will run # forever, even after this program exits. This is important for # other programs which might be using it. I don't really consider # this bad practice since the server is typically supposed to # be running all the time: start_daemon( [ sys.executable, '-m', 'zprocess.zlog', '--cls', 'RotatingFileHandler', '--maxBytes', str(maxBytes), '--backupCount', str(backupCount), ] ) # Try again. Longer timeout this time, give it time to start up: zprocess.zlog.connect(timeout=15) _connected_to_zlog = True
def main(): config = get_config() cmd = [ sys.executable, '-m', 'zprocess.zlog', '--port', str(config['zlog_port']), '--cls', 'RotatingFileHandler', '--maxBytes', str(config['logging_maxBytes']), '--backupCount', str(config['logging_backupCount']), '-l', LOG_PATH, ] if config['shared_secret_file'] is not None: cmd += ['--shared-secret-file', config['shared_secret_file']] elif config['allow_insecure']: cmd += ['--allow-insecure'] else: cmd += ['--no-allow-insecure'] if '--daemon' in sys.argv: start_daemon(cmd) else: try: subprocess.call(cmd) except KeyboardInterrupt: pass
def main(): config = get_config() cmd = [ sys.executable, '-m', 'zprocess.remote', '--port', str(config['zprocess_remote_port']), '-l', LOG_PATH, ] if config['shared_secret_file'] is not None: cmd += ['--shared-secret-file', config['shared_secret_file']] elif config['allow_insecure']: cmd += ['--allow-insecure'] else: cmd += ['--no-allow-insecure'] if '--daemon' in sys.argv: start_daemon(cmd) else: if '--no-tui' not in sys.argv: cmd += ['-tui'] try: subprocess.call(cmd) except KeyboardInterrupt: pass
def main(): config = get_config() if gethostbyname(config['zlock_host']) != gethostbyname('localhost'): msg = ( "Zlock not configured to run on this host according to labconfig: " + "zlock_host=%s" % config['zlock_host'] ) raise ValueError(msg) cmd = [ sys.executable, '-m', 'zprocess.zlock', '--port', config['zlock_port'], '-l', LOG_PATH, ] if config['shared_secret_file'] is not None: cmd += ['--shared-secret-file', config['shared_secret_file']] elif config['allow_insecure']: cmd += ['--allow-insecure'] else: cmd += ['--no-allow-insecure'] if '--daemon' in sys.argv: start_daemon(cmd) else: try: subprocess.call(cmd) except KeyboardInterrupt: pass