简体   繁体   English

FastAPI 响应迭代器正在阻止异步函数或后台任务

[英]FastAPI response iterator is blocking the async functions or background tasks

I wanted to get the response body in a middleware in FastAPI.我想在 FastAPI 的中间件中获取响应主体。 I tried the method by using the following answer on stackoverflow: https://stackoverflow.com/a/71883126/19869027 But When I use the following code in the middleware,我通过在 stackoverflow 上使用以下答案尝试了该方法: https://stackoverflow.com/a/71883126/19869027但是当我在中间件中使用以下代码时,
response_body = [section async for section in response.body_iterator]
it blocks the background tasks I use in fastapi routes, and response is not generated till background task finishes.它阻止了我在 fastapi 路由中使用的后台任务,并且在后台任务完成之前不会生成响应。 It works fine if I remove middleware.如果我删除中间件,它工作正常。 Kindly suggest some solution.请提出一些解决方案。 Regards问候

I've encountered similar problem.我遇到过类似的问题。 Turns out it was caused by a bug in Starlette.原来这是由 Starlette 中的错误引起的。 Bumping FastAPI to version 0.87.0 fixed the issue for me.将 FastAPI 升级到版本 0.87.0 为我解决了这个问题。 Based on a very helpful comment by r1b: https://github.com/encode/uvicorn/issues/111#issuecomment-1369272260基于 r1b 的非常有用的评论: https://github.com/encode/uvicorn/issues/111#issuecomment-1369272260

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM