-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
ASGI refactoring attempt #1475
ASGI refactoring attempt #1475
Conversation
@tomchristie since we've got X's across the board, how would you like to handle this? New PR after more ASGI work or update this guy when you're ready? |
Sure, so the blocker I ran into on first attempt was the test client. I figured the first thing is to switch the test client out so that it's pointing at the ASGI interface instead. Sanic uses an aiohttp-based test client, which makes actual network requests. I naively tried just swapping one out for the other, but there's enough API differences that it doesn't really fly. Anyways, the right way to progress this next would be...
Once that's sorted then we'll be able to add an ASGI interface onto Sanic, and run the existing test suite against it. I've got a few other bits on my plate at the moment, so it might take me a bit to get onto it, but defo on the TODO list. |
So I was digging a bit and it looks like the aiohttp folks are somewhat resistant to ASGI inclusion. Is it time to look at overhauling testing completely to be requests based in order to make us future-ready for doing ASGI? Personally I don't have it in me to go lobby over at aiohttp to push "closed" issues. |
Further, this looks like it's going to be a bigger chunk of work - do we want to split this off in to a feature branch that we can work on so more people can assist? @huge-success/sanic-core-devs |
Doesn't really matter from the POV of Sanic's Test Client. It wouldn't be in aiohttp's codebase.
It'd make the ASGI switch easier from my POV, but I don't think it's a blocker. (Conversly, if sanic does stay with aiohttp interface for now, then once we've made the ASGI switch, we'll get a requests-based alternative for free.) |
Any progress here or any blocker ? |
Yes. I am working on swapping out the test client to using |
@ahopkins Great - make sure to ping me when there's progress there, and I'll see if there's something I oughta be contributing from my side. |
@tomchristie I have a question about
If I add a new method, and pass that to
It looks to me like this is something that should be addressed. But in the mean time, I will press forward with something like the above. |
ASGISession works against the ASGI 3 interface - a single Async callable taking scope, receive, and send. This PR is a bit out of date since it targets the old double callable style. I can update this PR if that’s helpful, tho I think the first thing would be to update the underlying test client without considering ASGI. (Just making regular http calls direct to the app in the same way as the existing client) |
Makes sense, and that is basically what I am doing. But, I wanted to also test it against this work so far. |
@tomchristie Can you let me know if this is something you plan to pick up again with, or if you want me to continue on? Again, I really appreciate the work you have put in so far. |
Both options possible. I’ll try to take a look over where things are at in the coming days. |
any timeline on this ? |
I'm not able to put time on this this weekend. Bird my thought was next week to put some effort in to try and make it before the end of this month so we at least have it as an option for 19.6. |
Not ready for review yet. But progress nonetheless ... |
@ahopkins anything I can do to help? |
Just as a note here: https://community.sanicframework.org/t/june-19-6-0-release/310 I was suggesting on Jun-15, but I think as long as we get it out this month (before Jun-30) we're still following the release pattern. |
To complete:
|
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've not fully reviewed yet, but looks good overall.
Seems like still missing documentation around this, can we add that ? I'm thinking we probably should just mention ASGI support still in Beta for 19.6 release.
@@ -477,7 +477,7 @@ def write_response(self, response): | |||
async def drain(self): | |||
await self._not_paused.wait() | |||
|
|||
def push_data(self, data): | |||
async def push_data(self, data): |
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.
why do we add async
here ?
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'll go back and look again. I don't remember exactly, but I do remember there was a reason. I'll add a comment.
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.
The ASGI equivalent needs to be awaitable
. Therefore, I opted to make the Sanic version also awaitable
to reduce complexity. The alternative would be either adding an isawaitable
check, or a try/except
. I am up for changing it, but I thought the following in the StreamingHTTPResponse
was the simplest.
if self.chunked:
await self.protocol.push_data(b"%x\r\n%b\r\n" % (len(data), data))
else:
await self.protocol.push_data(data)
Yes, I did write some, but I don't know what happened to it. I will write again and push it. I agree with you. That's actually what I was suggesting we do. I put a section in deployment and then said that ASGI was in beta for 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.
I think we are ready to merge. Anyone else? @yunstanford? @sjsadowski?
I'll try to add some more testing coverage.
Sounds good, I'll take a look tonight. Thanks! |
Beautify Specify websockets version
Ahh.... |
Thanks, I'll prepare the release. |
Thanks @tomchristie for your help in pushing this forward. I hope this helps the overall Python community as we all continue to build a richer set of tools. |
@yunstanford @ahopkins - Great work folks! |
@ahopkins This is wonderful. Thank you so much @tomchristie Thanks |
This isn't intended to be graceful at this point, rather just to make a start on getting passing tests, using an ASGI-based app, connected to an ASGI-based test client.
The application changes are intended to be the dumbest possible thing that could provide the ASGI interface, with the intention that once the test suite was passing or sufficiently passing, then refactoring towards something far more graceful could start.
There are two changes:
Sanic
class has an ASGI based__call__
interface, that calls into a dumb shim layer.SanicTestClient
is replaced from anaiohttp
client that makes actual network requests, into arequests
based client, that has an adapter to make ASGI requests direct to the application.Because the
requests
interface differs in places to theaiohttp
client interface, there's a bit of fiddling around to ensure that API compatibility. Again, this is just the dumbest possible place to start, with a view to moving to something more graceful later.