Files
ray/python/ray/experimental/client/__init__.py
T
Barak Michener 2fe1321c3f [ray_client] __getattr__ for the API Import interface (#12089)
* move all things that import real-ray into the server folder

* change the import line and have a __getattr__-able API stub

* formatting

* remove unused (duplicated) util file

* Remove module methods (but leave comment on why)
2020-11-18 22:42:02 -08:00

52 lines
1.6 KiB
Python

from ray.experimental.client.api import ClientAPI
from ray.experimental.client.api import APIImpl
from typing import Optional
import logging
logger = logging.getLogger(__name__)
# _client_api has to be external to the API stub, below.
# Otherwise, ray.remote() that contains ray.remote()
# contains a reference to the RayAPIStub, therefore a
# reference to the _client_api, and then tries to pickle
# the thing.
_client_api: Optional[APIImpl] = None
class RayAPIStub:
def connect(self, conn_str):
global _client_api
from ray.experimental.client.worker import Worker
_client_worker = Worker(conn_str)
_client_api = ClientAPI(_client_worker)
def disconnect(self):
global _client_api
if _client_api is not None:
_client_api.close()
_client_api = None
def __getattr__(self, key: str):
global _client_api
self.__check_client_api()
return _client_api.__getattribute__(key)
def __check_client_api(self):
global _client_api
if _client_api is None:
from ray.experimental.client.server.core_ray_api import CoreRayAPI
_client_api = CoreRayAPI()
ray = RayAPIStub()
# Someday we might add methods in this module so that someone who
# tries to `import ray_client as ray` -- as a module, instead of
# `from ray_client import ray` -- as the API stub
# still gets expected functionality. This is the way the ray package
# worked in the past.
#
# This really calls for PEP 562: https://www.python.org/dev/peps/pep-0562/
# But until Python 3.6 is EOL, here we are.