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

@@ -13,6 +13,7 @@ sys.modules["__VC_HANDLER_MODULE_NAME"] = __vc_module
__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 = {}
@@ -198,16 +199,25 @@ elif 'app' in __vc_variables:
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)