Beispiel #1
0
    def _ray_serve_call(self, request):
        work_item = request

        if work_item.request_context == TaskContext.Web:
            serve_context.web = True
            asgi_scope, body_bytes = work_item.request_args
            flask_request = build_flask_request(asgi_scope, body_bytes)
            args = (flask_request, )
            kwargs = {}
        else:
            serve_context.web = False
            args = (FakeFlaskQuest(), )
            kwargs = work_item.request_kwargs

        result_object_id = work_item.result_object_id

        start_timestamp = time.time()
        try:
            result = self.__call__(*args, **kwargs)
            ray.worker.global_worker.put_object(result, result_object_id)
        except Exception as e:
            wrapped_exception = wrap_to_ray_error(e)
            self._serve_metric_error_counter += 1
            ray.worker.global_worker.put_object(wrapped_exception,
                                                result_object_id)
        self._serve_metric_latency_list.append(time.time() - start_timestamp)

        serve_context.web = False

        self._ray_serve_fetch()
Beispiel #2
0
    def _ray_serve_main_loop(self, my_handle):
        assert self._ray_serve_setup_completed
        self._ray_serve_self_handle = my_handle

        # Only retrieve the next task if we have completed previous task.
        if self._ray_serve_cached_work_token is None:
            work_token = ray.get(
                self._ray_serve_router_handle.dequeue_request.remote(
                    self._ray_serve_dequeue_requestr_name))
        else:
            work_token = self._ray_serve_cached_work_token

        work_token_id = ray.ObjectID(work_token)
        ready, not_ready = ray.wait([work_token_id],
                                    num_returns=1,
                                    timeout=0.5)
        if len(ready) == 1:
            work_item = ray.get(work_token_id)
            self._ray_serve_cached_work_token = None
        else:
            self._ray_serve_cached_work_token = work_token
            self._ray_serve_self_handle._ray_serve_main_loop.remote(my_handle)
            return

        if work_item.request_context == TaskContext.Web:
            serve_context.web = True
            asgi_scope, body_bytes = work_item.request_args
            flask_request = build_flask_request(asgi_scope, body_bytes)
            args = (flask_request, )
            kwargs = {}
        else:
            serve_context.web = False
            args = (FakeFlaskQuest(), )
            kwargs = work_item.request_kwargs

        result_object_id = work_item.result_object_id

        start_timestamp = time.time()
        try:
            result = self.__call__(*args, **kwargs)
            ray.worker.global_worker.put_object(result_object_id, result)
        except Exception as e:
            wrapped_exception = wrap_to_ray_error(e)
            self._serve_metric_error_counter += 1
            ray.worker.global_worker.put_object(result_object_id,
                                                wrapped_exception)
        self._serve_metric_latency_list.append(time.time() - start_timestamp)

        serve_context.web = False
        # The worker finished one unit of work.
        # It will now tail recursively schedule the main_loop again.

        # TODO(simon): remove tail recursion, ask router to callback instead
        self._ray_serve_self_handle._ray_serve_main_loop.remote(my_handle)
Beispiel #3
0
def parse_request_item(request_item):
    if request_item.request_context == TaskContext.Web:
        is_web_context = True
        asgi_scope, body_bytes = request_item.request_args
        flask_request = build_flask_request(asgi_scope, io.BytesIO(body_bytes))
        args = (flask_request, )
        kwargs = {}
    else:
        is_web_context = False
        args = (FakeFlaskRequest(), )
        kwargs = request_item.request_kwargs

    return args, kwargs, is_web_context
Beispiel #4
0
def parse_request_item(request_item):
    if request_item.request_context == TaskContext.Web:
        is_web_context = True
        asgi_scope, body_bytes = request_item.request_args

        # http_body_bytes enclosed in list due to
        # https://github.com/ray-project/ray/issues/6944
        # TODO(alind):  remove list enclosing after issue is fixed
        flask_request = build_flask_request(asgi_scope,
                                            io.BytesIO(body_bytes[0]))
        args = (flask_request, )
        kwargs = {}
    else:
        is_web_context = False
        args = (FakeFlaskRequest(), )
        kwargs = request_item.request_kwargs

    return args, kwargs, is_web_context
Beispiel #5
0
    def _ray_serve_main_loop(self, my_handle):
        assert self._ray_serve_setup_completed
        self._ray_serve_self_handle = my_handle

        work_token = ray.get(
            self._ray_serve_router_handle.dequeue_request.remote(
                self._ray_serve_dequeue_requestr_name))
        work_item = ray.get(ray.ObjectID(work_token))

        if work_item.request_context == TaskContext.Web:
            serve_context.web = True
            asgi_scope, body_bytes = work_item.request_args
            flask_request = build_flask_request(asgi_scope, body_bytes)
            args = (flask_request, )
            kwargs = {}
        else:
            serve_context.web = False
            args = (FakeFlaskQuest(), )
            kwargs = work_item.request_kwargs

        result_object_id = work_item.result_object_id

        try:
            result = self.__call__(*args, **kwargs)
            ray.worker.global_worker.put_object(result_object_id, result)
        except Exception as e:
            wrapped_exception = wrap_to_ray_error(e)
            ray.worker.global_worker.put_object(result_object_id,
                                                wrapped_exception)

        serve_context.web = False
        # The worker finished one unit of work.
        # It will now tail recursively schedule the main_loop again.

        # TODO(simon): remove tail recursion, ask router to callback instead
        self._ray_serve_self_handle._ray_serve_main_loop.remote(my_handle)