Skip to content
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

bugfix: ASGI support for newer python versions > 3.9 #11541

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/python/vc_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,33 @@ def __init__(self, scope):
self.state = ASGICycleState.REQUEST
self.app_queue = None
self.response = {}
self._legacy_asyncio = sys.version_info < (3, 10)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of creating a class member variable just for checking. How about we declare a local variable at the top of the __call__ function instead?


def __call__(self, app, body):
"""
Receives the application and any body included in the request, then builds the
ASGI instance using the connection scope.
Runs until the response is completely read from the application.
"""
loop = asyncio.new_event_loop()
self.app_queue = asyncio.Queue(loop=loop)
if self._legacy_asyncio:
loop = asyncio.new_event_loop()
self.app_queue = asyncio.Queue(loop=loop)
else:
self.app_queue = asyncio.Queue()
self.put_message({'type': 'http.request', 'body': body, 'more_body': False})

asgi_instance = app(self.scope, self.receive, self.send)

asgi_task = loop.create_task(asgi_instance)
loop.run_until_complete(asgi_task)
if self._legacy_asyncio:
asgi_task = loop.create_task(asgi_instance)
loop.run_until_complete(asgi_task)
else:
asyncio.run(self.run_asgi_instance(asgi_instance))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need a separate function? From what I see in the handler, at this point in time, we already know that app (or app.__call__) is a coroutine. Can't we just run it directly instead?

Suggested change
asyncio.run(self.run_asgi_instance(asgi_instance))
asyncio.run(asgi_instance)

return self.response

async def run_asgi(self, asgi_instance):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this should be run_asgi_instance as it appears line 215 is trying to call it?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@winglian - just checking to see if you saw this?

await asgi_instance

def put_message(self, message):
self.app_queue.put_nowait(message)

Expand Down