Is there any way to have a route handler that can return both a Stream or a str? #1566
Answered
by
Goldziher
danechitoaie
asked this question in
Q&A
-
Here's my code from asyncio import sleep
from collections.abc import AsyncGenerator
from starlite import Starlite, Stream, get
from starlite import BackgroundTask
TEXT = "Hello World!"
async def my_generator() -> AsyncGenerator[str, None]:
for c in TEXT:
await sleep(1)
yield str(c)
@get(path="/stream")
def stream_time(s: bool = False) -> Stream | str:
return Stream(iterator=my_generator(), media_type="text/plain") if s else TEXT
app = Starlite(route_handlers=[stream_time], debug=True) I'd like to either stream the response or to just return it directly as a
I tried added
|
Beta Was this translation helpful? Give feedback.
Answered by
Goldziher
Apr 21, 2023
Replies: 1 comment
-
Hi. This is not supported. The reason is that we precompute the routes to ensure performance and this will make this very complex. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
danechitoaie
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. This is not supported. The reason is that we precompute the routes to ensure performance and this will make this very complex.