Esempio n. 1
0
        def __init__(self, backend_tag, replica_tag, init_args,
                     backend_config: BackendConfig, controller_name: str):
            if isinstance(backend_def, str):
                backend = import_attr(backend_def)
            else:
                backend = backend_def

            if inspect.isfunction(backend):
                is_function = True
            elif inspect.isclass(backend):
                is_function = False
            else:
                assert False, ("backend_def must be function, class, or "
                               "corresponding import path.")

            # Set the controller name so that serve.connect() in the user's
            # backend code will connect to the instance that this backend is
            # running in.
            ray.serve.api._set_internal_replica_context(
                backend_tag, replica_tag, controller_name)
            if is_function:
                _callable = backend
            else:
                _callable = backend(*init_args)

            assert controller_name, "Must provide a valid controller_name"
            controller_handle = ray.get_actor(controller_name)
            self.backend = RayServeReplica(_callable, backend_config,
                                           is_function, controller_handle)
Esempio n. 2
0
def test_import_attr():
    assert import_attr("ray.serve.Client") == ray.serve.api.Client
    assert import_attr("ray.serve.api.Client") == ray.serve.api.Client

    policy_cls = import_attr("ray.serve.controller.TrafficPolicy")
    assert policy_cls == ray.serve.controller.TrafficPolicy

    policy = policy_cls({"endpoint1": 0.5, "endpoint2": 0.5})
    with pytest.raises(ValueError):
        policy.set_traffic_dict({"endpoint1": 0.5, "endpoint2": 0.6})
    policy.set_traffic_dict({"endpoint1": 0.4, "endpoint2": 0.6})

    print(repr(policy))

    # Very meta...
    import_attr_2 = import_attr("ray.serve.utils.import_attr")
    assert import_attr_2 == import_attr
Esempio n. 3
0
        async def __init__(self, backend_tag, replica_tag, init_args,
                           backend_config: BackendConfig,
                           controller_name: str):
            if isinstance(backend_def, str):
                backend = import_attr(backend_def)
            else:
                backend = backend_def

            if inspect.isfunction(backend):
                is_function = True
            elif inspect.isclass(backend):
                is_function = False
            else:
                assert False, ("backend_def must be function, class, or "
                               "corresponding import path.")

            # Set the controller name so that serve.connect() in the user's
            # backend code will connect to the instance that this backend is
            # running in.
            ray.serve.api._set_internal_replica_context(
                backend_tag,
                replica_tag,
                controller_name,
                servable_object=None)
            if is_function:
                _callable = backend
            else:
                _callable = backend(*init_args)
            # Setting the context again to update the servable_object.
            ray.serve.api._set_internal_replica_context(
                backend_tag,
                replica_tag,
                controller_name,
                servable_object=_callable)

            self.shutdown_hook: Optional[Callable] = None
            if backend_config.internal_metadata.is_asgi_app:
                app = _callable._serve_asgi_app
                startup_hook, self.shutdown_hook = make_startup_shutdown_hooks(
                    app)
                await startup_hook()

            assert controller_name, "Must provide a valid controller_name"
            controller_handle = ray.get_actor(controller_name)
            self.backend = RayServeReplica(_callable, backend_config,
                                           is_function, controller_handle)
Esempio n. 4
0
        async def __init__(self, backend_tag, replica_tag, init_args,
                           backend_config: BackendConfig,
                           controller_name: str):
            if isinstance(backend_def, str):
                backend = import_attr(backend_def)
            else:
                backend = backend_def

            if inspect.isfunction(backend):
                is_function = True
            elif inspect.isclass(backend):
                is_function = False
            else:
                assert False, ("backend_def must be function, class, or "
                               "corresponding import path.")

            # Set the controller name so that serve.connect() in the user's
            # backend code will connect to the instance that this backend is
            # running in.
            ray.serve.api._set_internal_replica_context(backend_tag,
                                                        replica_tag,
                                                        controller_name,
                                                        servable_object=None)
            if is_function:
                _callable = backend
            else:
                # This allows backends to define an async __init__ method
                # (required for FastAPI backend definition).
                _callable = backend.__new__(backend)
                await sync_to_async(_callable.__init__)(*init_args)
            # Setting the context again to update the servable_object.
            ray.serve.api._set_internal_replica_context(
                backend_tag,
                replica_tag,
                controller_name,
                servable_object=_callable)

            assert controller_name, "Must provide a valid controller_name"
            controller_handle = ray.get_actor(controller_name)
            self.backend = RayServeReplica(_callable, backend_config,
                                           is_function, controller_handle)