Conversation
b64a9c4 to
030451c
Compare
d282960 to
9f6fa92
Compare
c5af9c7 to
0fd9548
Compare
|
just a drive by comment: this is cool - would love to use this in a Starlette app. i will build some prototypes off this branch to get a feel for it, but i think asyncio support would be very useful for me. |
|
@raisjn cool, thanks for dropping the comment!
|
|
i'm still playing with htpy (cool!) and async iteration. the code looks fine to me, but will report back after more days (weeks?) of testing and trying to build the below functionality: comments on async delivery: from what i can tell, it resolves the work in sequential order. what i am aiming for is pipelined rendering (also known as partial pre-rendering in next.js). I believe pipelined delivery can be built on top of (or alongside) htpy's async rendering by using some JS + an async queue. it would look something like this:
|
|
Interesting! I think just sending CSS/JS before the full page is rendered gives a good boost for free for anyone. Streaming multiple parts of the page and then recombining it with javascript sounds interesting! It feels like there should be a small lib for that. It feels simple in theory! 😆 |
i agree - this is a great boost! i'm not 100% sure using the exact API doesn't matter as long as it is explicit, it could also be a special tag ( |
|
why would a explicit flush function/tag be useful? currently with this PR, everything will be "flushed" as soon as it is ready anyways. |
|
coming back to this: it turns out i don't need async iterators in htpy, have architected an app with async delivery that allows parallel pipelining instead of sequential and uses htpy without async. in my first reading of this PR, i had assumed async iter was in parallel. (collect all async in one pass, then await them in parallel, then do next set, etc). re: why use explicit flush: i think it would help for people who aren't aware of the mechanics of rendering and using async generator vs sync generator. |
98b951c to
b6b0193
Compare
|
I started writing I have a local fork with this branch merged with # ipython for top-level `async for`
async def my_component():
return p["hello!"]
my_div = div[my_component()]
async for chunk in my_div:
print(chunk)
# <div>
# <p>
# Hello!
# </p>
# </div>
my_fragment = fragment[my_component()]
async for chunk in my_fragment:
print(chunk)
# TypeError: 'async for' requires an object with __aiter__ method, got Fragment
# ^this one makes sense
my_fragment_in_div = div[fragment[my_component()]]
async for chunk in my_fragment_in_div:
print(chunk)
# <div>
# Traceback (most recent call last):
# ...
# ValueError: <coroutine object f at 0x1042f3110> is not a valid child element. Use async iteration to retrieve element content: https://htpy.dev/streaming/full tracebackIf you look at that stack trace, it looks as though the rendering method on Thank you for your great work on this very enjoyable library! |
|
cool, thanks for the feedback and picking up the work on this! have not been pursuing async iteration since I do not use it myself no-one showed (enough) intereste in using it yet! there probably needs to be an explicit check for Any feedback on the docs/explaination about async and streaming (both sync and async) would be appreciated. I would like to merge #92 first and then I can fix the last bits here. Btw, #92 changes the chunk iteration from plain |
|
Personally I'm very interested in async iteration, and would encourage you to release async support once it's rectified with the new iteration protocol. The ability to delay expensive work and start streaming a response NOW--simply by dropping awaitables and async generators into my htpy expressions--has been a huge boon for my app (which is specifically geared towards users on high latency/ packet-loss connections). I like Happy to play around with this on my local branch and think about documentation (time permitting) over the next few days! |
| @@ -14,43 +14,40 @@ client while the page is being generated. | |||
| streaming will be the easiest way to get going. Streaming can give you | |||
There was a problem hiding this comment.
Improve these docs:
- Use the same example/output for for both sync and async examples
- Show the example code first in an inline code block instead of link to it. Show the video directly below.
- Remove/shorten the section on callables/lambda and generators.
|
See #160 for a updated PR. |
This PR adds the possibility to use async awaitables/iterators/generators to generate a response. A sample Starlette example is added too. Thoughts? Ideas?