diff --git a/doc/source/api.rst b/doc/source/api.rst index 897f30c2c..3c16cc8a8 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -3,6 +3,8 @@ The Ray API .. autofunction:: ray.init +.. autofunction:: ray.is_initialized + .. autofunction:: ray.remote .. autofunction:: ray.get diff --git a/python/ray/__init__.py b/python/ray/__init__.py index 1fcff76f4..a56531e3e 100644 --- a/python/ray/__init__.py +++ b/python/ray/__init__.py @@ -50,7 +50,8 @@ from ray.local_scheduler import ObjectID, _config # noqa: E402 from ray.profiling import profile # noqa: E402 from ray.worker import (error_info, init, connect, disconnect, get, put, wait, remote, get_gpu_ids, get_resource_ids, get_webui_url, - register_custom_serializer, shutdown) # noqa: E402 + register_custom_serializer, shutdown, + is_initialized) # noqa: E402 from ray.worker import (SCRIPT_MODE, WORKER_MODE, LOCAL_MODE, PYTHON_MODE) # noqa: E402 from ray.worker import global_state # noqa: E402 @@ -67,9 +68,10 @@ __version__ = "0.5.2" __all__ = [ "error_info", "init", "connect", "disconnect", "get", "put", "wait", "remote", "profile", "actor", "method", "get_gpu_ids", "get_resource_ids", - "get_webui_url", "register_custom_serializer", "shutdown", "SCRIPT_MODE", - "WORKER_MODE", "LOCAL_MODE", "PYTHON_MODE", "global_state", "ObjectID", - "_config", "__version__", "internal" + "get_webui_url", "register_custom_serializer", "shutdown", + "is_initialized", "SCRIPT_MODE", "WORKER_MODE", "LOCAL_MODE", + "PYTHON_MODE", "global_state", "ObjectID", "_config", "__version__", + "internal" ] import ctypes # noqa: E402 diff --git a/python/ray/worker.py b/python/ray/worker.py index 83e127126..9d04aa137 100644 --- a/python/ray/worker.py +++ b/python/ray/worker.py @@ -1987,6 +1987,15 @@ def print_error_messages_raylet(worker): pass +def is_initialized(): + """Check if ray.init has been called yet. + + Returns: + True if ray.init has already been called and false otherwise. + """ + return ray.worker.global_worker.connected + + def print_error_messages(worker): """Print error messages in the background on the driver. diff --git a/test/runtest.py b/test/runtest.py index 8520d6885..daa5e619d 100644 --- a/test/runtest.py +++ b/test/runtest.py @@ -2536,5 +2536,21 @@ class GlobalStateAPI(unittest.TestCase): ray.experimental.flush_evicted_objects_unsafe() +class InitializationTest(unittest.TestCase): + def tearDown(self): + ray.shutdown() + assert not ray.is_initialized() + + def testInitialized(self): + assert not ray.is_initialized() + ray.init(num_cpus=0) + assert ray.is_initialized() + + def testInitializedLocalMode(self): + assert not ray.is_initialized() + ray.init(num_cpus=0, local_mode=True) + assert ray.is_initialized() + + if __name__ == "__main__": unittest.main(verbosity=2)