mirror of
https://github.com/wassname/ray.git
synced 2026-07-01 02:32:08 +08:00
2fe1321c3f
* 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)
52 lines
1.6 KiB
Python
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.
|