-
-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to asyncio streams API #869
Conversation
21557c0
to
839e015
Compare
@euri10 What are we thinking about this…? I'd love for this small bit to get in for 0.14.0. We could also some kind of public beta thing so people try it out in production settings. I could understand that we'd be a bit wary about this "run protocols from streams" thing, so that could help us validate if that's a sensible thing to do / doesn't come with strange edge cases. (Personally I'm quite confident that it can work just fine, just saying I would understand if we'd want to be more risk-averse.) |
839e015
to
53c9e95
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth taking a shot at it @florimondmanca
It looks like a good 1st step towards introducing trio,
I just have a comment below, I'm not sure at all about its pertinence, but I better look stupid than say nothing, but I'm always kind of careful when we speak about read and write buffers and networking, it reminds me nightmare memory issues 🦞
Interestingly, I played a little with it and get not a bug but something we've been on recently:
it's happening just when specifying asyncio loop, putting uvloop and we dont have that |
53c9e95
to
8820440
Compare
@euri10 Yup, just saw that (I think it surfaced thanks to the upgraded test suite — I wasn't seeing it before). Resolved in the latest (force) push: we used to naively |
@euri10 How would you go about confirming there's no risk of memory issue with this…? Spawn a server, hit it with random requests for a while, and see how memory usage goes? |
@euri10 Hmm nah, my latest attempt isn't working. Try requesting with |
917d610
to
e0c6b83
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some more work on swapping initial read buffers and exception handling, I think this is looking good. :)
9b9f7a1
to
6c333c2
Compare
Having a client like this is a good start (shamelessly taken from here)
I just played a little with it, it seems I have same results on master so I doesn't seem specific to this branch, on both I stopped a 1M requests (this is where the plateau begins, way shorter in 2nd graph), but in both cases we can see memory usage growing up: I used this
|
I love this, just have 2 small questions and I feel we gtg |
@euri10 Thanks for the quick memory benchmarks! About the growing memory — shouldn't the client also be reading the response from the socket? Uvicorn responds anyway so I'm wondering if not reading the response means it gets buffered indefinitely on the server side. Do you get the same behavior if using a full fledged client like HTTPX? |
yep that's the whole point of this stupid client if I understand it correctly. but that's not because of that branch so we can track this as a separate thing !
|
hi @florimondmanca , fancy rebasing this ? |
@euri10 Putting this on my TODO list. Hope to give it a go today or beginning of next week… |
@euri10 Should be ready now. :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @florimondmanca !
* Switch to asyncio streams API * Tweak buffer swapping * Properly handle exceptions * More explanatory comments, 3.6 compatibility * Drop unused MAX_RECV
Refs #169
Okay, this is an ever more stripped-down alternative to #867 that I think we can consider for merging (at last!).
Description
This PR switches the
Server
from starting asyncio servers using protocols, to starting asyncio servers using a streams handler (protocols API -> streams API).The rest is glue code: we pass a handler that defers to our existing HTTP protocol classes. It's a bit of a clever hack, but I've left comments that I hope are sufficiently helpful.
Motivation
Switching to the
asyncio
streams API is required before abstracting awayasyncio
specifics, and introducingtrio
orcurio
support.This "reuse existing protocols implementations" is actually nice, because we get to keep the existing asyncio-optimized code, yet this paves the way for async agnostic support.
Next steps
The next steps after this PR would be:
asyncio
. Abstract asyncio away in server management #870trio
/curio
through a new implementation. (Eventually we can also consider havingasyncio
in there, as a new implementation.)