support newer python versions (#11675)

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. https://github.com/vercel/vercel/pull/11541 mostly fixed it, with some comments.

OP hasn't replied to feedback on that PR, so I've take it over from them.
This commit is contained in:
Trek Glowacki
2024-05-30 11:46:28 -07:00
committed by GitHub
parent 83741a0eb9
commit 3eb9d8c892
2 changed files with 19 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@vercel/python": minor
---
support newer python versions

View File

@@ -13,6 +13,7 @@ sys.modules["__VC_HANDLER_MODULE_NAME"] = __vc_module
__vc_spec.loader.exec_module(__vc_module) __vc_spec.loader.exec_module(__vc_module)
__vc_variables = dir(__vc_module) __vc_variables = dir(__vc_module)
_use_legacy_asyncio = sys.version_info < (3, 10)
def format_headers(headers, decode=False): def format_headers(headers, decode=False):
keyToList = {} keyToList = {}
@@ -198,16 +199,25 @@ elif 'app' in __vc_variables:
ASGI instance using the connection scope. ASGI instance using the connection scope.
Runs until the response is completely read from the application. Runs until the response is completely read from the application.
""" """
loop = asyncio.new_event_loop() if _use_legacy_asyncio:
self.app_queue = asyncio.Queue(loop=loop) 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}) self.put_message({'type': 'http.request', 'body': body, 'more_body': False})
asgi_instance = app(self.scope, self.receive, self.send) asgi_instance = app(self.scope, self.receive, self.send)
asgi_task = loop.create_task(asgi_instance) if _use_legacy_asyncio:
loop.run_until_complete(asgi_task) 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 return self.response
async def run_asgi_instance(self, asgi_instance):
await asgi_instance
def put_message(self, message): def put_message(self, message):
self.app_queue.put_nowait(message) self.app_queue.put_nowait(message)