From 975bfe89ad1cc0cf4be165454e0fbb77fb3f5c98 Mon Sep 17 00:00:00 2001 From: Robert Nishihara Date: Wed, 27 Jul 2016 11:20:37 -0700 Subject: [PATCH] check that we are connected before calling ray commands (#303) --- doc/install-on-macosx.md | 4 ++-- doc/install-on-ubuntu.md | 4 ++-- lib/python/ray/worker.py | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/doc/install-on-macosx.md b/doc/install-on-macosx.md index 29daca152..c7c30d464 100644 --- a/doc/install-on-macosx.md +++ b/doc/install-on-macosx.md @@ -34,8 +34,8 @@ source setup-env.sh # Add Ray to your Python path. ``` For convenience, you may also want to add the line `source -"$RAY_ROOT/setup-env.sh"` to your `~/.bashrc` file manually, where `$RAY_ROOT` -is the Ray directory (e.g., `/home/ubuntu/ray`). +"$RAY_ROOT/setup-env.sh"` to the bottom of your `~/.bashrc` file manually, where +`$RAY_ROOT` is the Ray directory (e.g., `/home/ubuntu/ray`). ### Test if the installation succeeded diff --git a/doc/install-on-ubuntu.md b/doc/install-on-ubuntu.md index f14cbebda..ec87fdf7f 100644 --- a/doc/install-on-ubuntu.md +++ b/doc/install-on-ubuntu.md @@ -30,8 +30,8 @@ source setup-env.sh # Add Ray to your Python path. ``` For convenience, you may also want to add the line `source -"$RAY_ROOT/setup-env.sh"` to your `~/.bashrc` file manually, where `$RAY_ROOT` -is the Ray directory (e.g., `/home/ubuntu/ray`). +"$RAY_ROOT/setup-env.sh"` to the bottom of your `~/.bashrc` file manually, where +`$RAY_ROOT` is the Ray directory (e.g., `/home/ubuntu/ray`). ### Test if the installation succeeded diff --git a/lib/python/ray/worker.py b/lib/python/ray/worker.py index 5893eb3e3..0f84c34f8 100644 --- a/lib/python/ray/worker.py +++ b/lib/python/ray/worker.py @@ -437,6 +437,15 @@ reinitialize these variables after they are used so that changes to their state made by one task do not affect other tasks. """ +def check_connected(worker=global_worker): + """Check if the worker is connected. + + Raises: + Exception: An exception is raised if the worker is not connected. + """ + if worker.handle is None: + raise Exception("This command cannot be called before a Ray cluster has been started. You can start one with 'ray.services.start_ray_local(num_workers=1)'.") + def print_failed_task(task_status): """Print information about failed tasks. @@ -479,6 +488,7 @@ def print_task_info(task_data, mode): def scheduler_info(worker=global_worker): """Return information about the state of the scheduler.""" + check_connected(worker) return ray.lib.scheduler_info(worker.handle) def visualize_computation_graph(file_path=None, view=False, worker=global_worker): @@ -499,7 +509,7 @@ def visualize_computation_graph(file_path=None, view=False, worker=global_worker >>> z = da.dot(x, y) >>> ray.visualize_computation_graph(view=True) """ - + check_connected(worker) if file_path is None: file_path = ray.config.get_log_file_path("computation-graph.pdf") @@ -519,6 +529,7 @@ def visualize_computation_graph(file_path=None, view=False, worker=global_worker def task_info(worker=global_worker): """Return information about failed tasks.""" + check_connected(worker) return ray.lib.task_info(worker.handle) def register_module(module, worker=global_worker): @@ -530,6 +541,7 @@ def register_module(module, worker=global_worker): args: module (module): The module of functions to register. """ + check_connected(worker) logging.info("registering functions in module {}.".format(module.__name__)) for name in dir(module): val = getattr(module, name) @@ -574,6 +586,7 @@ def disconnect(worker=global_worker): # Reset the list of cached remote functions so that if more remote functions # are defined and then connect is called again, the remote functions will be # exported. This is mostly relevant for the tests. + worker.handle = None worker.cached_remote_functions = [] reusables._cached_reusables = [] @@ -591,6 +604,7 @@ def get(objref, worker=global_worker): Returns: A Python object """ + check_connected(worker) if worker.mode == ray.PYTHON_MODE: return objref # In ray.PYTHON_MODE, ray.get is the identity operation (the input will actually be a value not an objref) ray.lib.request_object(worker.handle, objref) @@ -610,6 +624,7 @@ def put(value, worker=global_worker): Returns: The object reference assigned to this value. """ + check_connected(worker) if worker.mode == ray.PYTHON_MODE: return value # In ray.PYTHON_MODE, ray.put is the identity operation objref = ray.lib.get_objref(worker.handle) @@ -780,6 +795,7 @@ def remote(arg_types, return_types, worker=global_worker): def remote_decorator(func): def func_call(*args, **kwargs): """This gets run immediately when a worker calls a remote function.""" + check_connected() args = list(args) args.extend([kwargs[keyword] if kwargs.has_key(keyword) else default for keyword, default in keyword_defaults[len(args):]]) # fill in the remaining arguments if _mode() == ray.PYTHON_MODE: