Flask 支持 async def 视图,它到底带来了什么?每个请求是不是都在同一个事件循环里?在 gunicorn 下能同时处理更多请求吗?这篇用真实的 asyncio 与 gunicorn 把边界量出来。
GET /s → sync | GET /a → async 两个请求里事件循环的 id 相同吗: False | 线程相同吗: False | 视图里能用 request.path: /loop 一个请求里:asyncio.gather 三个各 0.5 秒的等待 → 0.5 秒;依次 await 三次 → 1.51 秒
上一篇:Flask 同步请求模型;下一篇:Async View 的执行边界。
sync 视图 69 µs/请求;async 视图(await 一个 sleep(0))320 µs/请求;多出约 251 µs
gunicorn sync,1 个 worker async 视图 8.07 秒 | sync 视图 8.04 秒 | 全部 200:True gunicorn gthread,1 个 worker × 8 线程 async 视图 1.03 秒 | sync 视图 1.02 秒 | 全部 200:True