def blocking_cluster(): """Start a dask LocalCluster and block until iterrupted""" parser = argparse.ArgumentParser( description="Start a new Dask local cluster") parser.add_argument("-p", "--port", help="Port to use for the Dask local cluster", dest="port") args = parser.parse_args() if args.port: port = int(args.port) else: scheduler_address = cfg.get("cluster", "scheduler_address") port = int(scheduler_address.split(":")[-1]) try: n_workers = int(cfg.get("cluster", "n_workers")) local_cluster = LocalCluster(scheduler_port=port, n_workers=n_workers) print(f"Started local cluster at {local_cluster.scheduler_address}") except OSError as e: print(f"Could not start local cluster with at port: {port}") raise try: loop = True while loop: try: time.sleep(2) except KeyboardInterrupt: print("Interrupted") loop = False finally: local_cluster.close()
def default_cluster(**kwargs): """Start a dask LocalCluster at the scheduler port given by the config kwargs: override defaults """ scheduler_address = cfg.get("cluster", "scheduler_address") port = int(scheduler_address.split(":")[-1]) settings = { "scheduler_port": port, "n_workers": int(cfg.get("cluster", "n_workers")), } settings.update(kwargs) cluster = LocalCluster(**settings) return cluster
def default_client(timeout="2s", **kwargs): """Return Dask client at scheduler adress as defined by the global config""" scheduler_address = cfg.get("cluster", "scheduler_address") try: client = Client(scheduler_address, timeout=timeout, **kwargs) return client except (TimeoutError, IOError): print( f"No valid Dask scheduler found at specified address: '{scheduler_address}'" ) return False
def run_apps(): np.random.seed(43) torch.manual_seed(43) scheduler_address = cfg.get("cluster", "scheduler_address") if not verify_cluster(scheduler_address): print( f"No valid Dask scheduler found at specified address: '{scheduler_address}'" ) return log_root_dir = Path.home() / ".pyhdx" / "logs" log_dir = log_root_dir / datetime.datetime.now().strftime("%Y%m%d") log_dir.mkdir(parents=True, exist_ok=True) # catch error when log dir does not exist root_log = logging.getLogger("pyhdx") root_log.setLevel(logging.DEBUG) fh = logging.FileHandler(log_dir / "pyhdx_logs.txt") formatter = logging.Formatter( "%(asctime)s %(name)s [%(levelname)s]: %(message)s", "%Y-%m-%d %H:%M:%S") fh.setFormatter(formatter) fh.setLevel(logging.DEBUG) root_log.addHandler(fh) root_log.info("Starting PyHDX server") tornado_logger = logging.getLogger("tornado.application") fh = logging.FileHandler(log_dir / "tornado_logs.txt") formatter = logging.Formatter( "%(asctime)s %(name)s [%(levelname)s]: %(message)s", "%Y-%m-%d %H:%M:%S") fh.setFormatter(formatter) fh.setLevel(10) tornado_logger.addHandler(fh) #TODO Clean assets dir from pdb files Path(cfg.assets_dir).mkdir(exist_ok=True, parents=True) print("Welcome to the PyHDX server!") pn.serve( APP_DICT, static_dirs={ "pyhdx": STATIC_DIR, "assets": str(cfg.assets_dir) }, index=str(STATIC_DIR / "index.html"), )
def main(): parser = argparse.ArgumentParser(prog="pyhdx", description="PyHDX Launcher") parser.add_argument("serve", help="Runs PyHDX Dashboard") parser.add_argument("--scheduler_address", help="Run with local cluster <ip>:<port>") args = parser.parse_args() if args.scheduler_address: ip, port = args.scheduler_address.split(":") if not ip_address(ip): print("Invalid IP Address") return elif not 0 <= int(port) < 2**16: print("Invalid port, must be 0-65535") return cfg.set("cluster", "scheduler_address", args.scheduler_address) scheduler_address = cfg.get("cluster", "scheduler_address") if not verify_cluster(scheduler_address): # Start a new local cluster if none is found client = default_cluster() _, ip, port = client.scheduler_address.split(":") ip = ip.strip("/") scheduler_address = f"{ip}:{port}" print(f"Started new Dask LocalCluster at {scheduler_address}") if args.serve: serve.run_apps() loop = True while loop: try: time.sleep(1) except KeyboardInterrupt: print("Interrupted") loop = False