-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[serve] Add experimental support for
StreamingResponse
using `RayOb…
…jectRefGenerator` (#35720) Adds experimental support for using `StreamingResponse`s to stream intermediate results back to the client. This is currently gated behind a feature flag (must set `RAY_SERVE_ENABLE_EXPERIMENTAL_STREAMING=1`. This is implemented by using the Ray `ObjectRefStreamingGenerator` interface. When the feature flag is on, the HTTP proxy will use `num_returns="streaming"` for _all_ calls to downstream replicas. The replica code has been modified to incrementally yield raw ASGI messages back to the HTTP proxy. Known limitations & follow-ups (to be addressed before a non-experimental release): - Minor performance regression due to an extra RPC from streaming protocol (see the microbenchmark results posted on #35468). Most of this should be able to be optimized away before turning this on by default. - Streaming is not yet possible using the `ServeHandle` interface: #35777 - `max_concurrent_queries` is not respected by the HTTP proxy when streaming is enabled; we do simple round-robin instead: #35778 - The timeout set in the HTTP proxy does not apply to streaming responses: #35779
- Loading branch information
Showing
19 changed files
with
860 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# flake8: noqa | ||
|
||
# __begin_example__ | ||
import time | ||
from typing import Generator | ||
|
||
import requests | ||
from starlette.responses import StreamingResponse | ||
from starlette.requests import Request | ||
|
||
from ray import serve | ||
|
||
|
||
@serve.deployment | ||
class StreamingResponder: | ||
def generate_numbers(self, max: int) -> Generator[str, None, None]: | ||
for i in range(max): | ||
yield str(i) | ||
time.sleep(0.1) | ||
|
||
def __call__(self, request: Request) -> StreamingResponse: | ||
max = request.query_params.get("max", "25") | ||
gen = self.generate_numbers(int(max)) | ||
return StreamingResponse(gen, status_code=200, media_type="text/plain") | ||
|
||
|
||
serve.run(StreamingResponder.bind()) | ||
|
||
r = requests.get("http://localhost:8000?max=10", stream=True) | ||
start = time.time() | ||
r.raise_for_status() | ||
for chunk in r.iter_content(chunk_size=None, decode_unicode=True): | ||
print(f"Got result {round(time.time()-start, 1)}s after start: '{chunk}'") | ||
# __end_example__ | ||
|
||
|
||
r = requests.get("http://localhost:8000?max=10", stream=True) | ||
r.raise_for_status() | ||
for i, chunk in enumerate(r.iter_content(chunk_size=None, decode_unicode=True)): | ||
assert chunk == str(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.