This is a copy of @vercel/[email protected] except with 2 things changed in vc_init.py
:
- Uses a shared event loop.
Vercel does
asyncio.new_event_loop()
every new request, which leads toRuntimeError
being raised if 2 requests use/create a resource that was created on a different event loop, for exampleasyncio.Tasks
and a globalaiohttp.ClientSession
object. It also looks like Vercel makes every new request go through the sync functionvc_handler()
. So we run an event loop on a separate thread and make all requests use that one loop.
- Runs the
__aenter__
part of lifespan.
Vercel does not support lifespan. Lifespan is used init async code, like creating a database connection pool. This runs once per instance, not per request, so whatever is in there contributes to the coldstart time. A reference is kept so the Garbage Collector won't silently raise
GeneratorExit
on it. Finally, this only supports Starlette's lifespan which located inapp.router.lifespan_context
. This part will be skipped if no lifespan was found.
In your vercel.json
file, change the npm package to use to @imptype/[email protected]
, for example:
{
"builds": [
{
"src": "main.py",
"use": "@imptype/[email protected]"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "main.py"
}
]
}
- 3.4.12 - Uses shared event loop
- 3.4.35 - Uses shared event loop and runs the
__aenter__
part of lifespan