Skip to content

Commit

Permalink
support newer python versions (#11675)
Browse files Browse the repository at this point in the history
When we added the option of getting `al2023` in addition to `al2`, this required a python version bump for those apps on `al2023`. Python 3.10 had a breaking minor change. #11541 mostly fixed it, with some comments.

OP hasn't replied to feedback on that PR, so I've take it over from them.
  • Loading branch information
trek committed May 30, 2024
1 parent 83741a0 commit 3eb9d8c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-moles-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vercel/python": minor
---

support newer python versions
18 changes: 14 additions & 4 deletions packages/python/vc_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__vc_spec.loader.exec_module(__vc_module)
__vc_variables = dir(__vc_module)

_use_legacy_asyncio = sys.version_info < (3, 10)

def format_headers(headers, decode=False):
keyToList = {}
Expand Down Expand Up @@ -198,16 +199,25 @@ def __call__(self, app, body):
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 _use_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 _use_legacy_asyncio:
asgi_task = loop.create_task(asgi_instance)
loop.run_until_complete(asgi_task)
else:
asyncio.run(self.run_asgi_instance(asgi_instance))
return self.response

async def run_asgi_instance(self, asgi_instance):
await asgi_instance

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

Expand Down

0 comments on commit 3eb9d8c

Please sign in to comment.