Unclear how to create a background task with dependencies from within the handler scope #765
-
This seems related to #666 but also more specific than that discussion. Both Starlette and FastAPI have you define background tasks within the view and to pass it as the background parameter to the response object. This seems to be correctly scoped as it allows one to do background things with arbitrary resources that have been variously injected into, created within, or retrieved within the handling scope. In Starlite, because the background function is referenced in the route decorator, it is not clear how one would pass in locally scoped resources as arguments to the background task. The immediate solution that comes to mind is to return a Starlette JSONResponse and use the above mentioned pattern of specifying the background parameter in the response. However, I am currently having issues with rendering Starlette's JSONResponse and I think it may break the openapi docs if I do get it working (I'm not entirely sure about this latter point). I am guessing that replacing Starlette responses will be the change that enables the pattern I am looking for here. Should I just wait for that, or is there a currently known approach for what I am trying to do with background tasks? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
This would be a documentation issue. You can add background tasks to your response as well, see the reference docs here. So you can do something like from starlite import Response, get, BackgroundTask
async def do_something_with_id(id: str) -> None:
...
@get("/with-background/{id:str}")
async def with_background(id: str) -> Response:
return Response(
content="",
background=BackgroundTask(do_something_with_id, id=id),
) It's not advertised in the section about background tasks though, which it should be. I guess that's be something that should be covered in a tutorial section on background tasks, what do you think? |
Beta Was this translation helpful? Give feedback.
-
As a follow-up, I am wondering if there is a way to get the openapi docs to display a specific response schema, and for that matter to get response validation to work. My thought was that I could pass the response_class to the route decorator, but that does not appear to affect anything. I tried something like this:
... thinking that the generic Response is required if I want to pass in a background task, but that the |
Beta Was this translation helpful? Give feedback.
-
I realize now that my above example conflates the idea of a serializer with the idea of a response, so I tried this with a subclass with specified serializer instead:
But I still don't get rendered documentation or validation. |
Beta Was this translation helpful? Give feedback.
-
Ah. I got it! It's all very straightforward once you understand the pieces. I did something like this (no subclass required!)
This should result both in proper OpenAPI documentation for the endpoint response as well as proper validation of the response. |
Beta Was this translation helpful? Give feedback.
This would be a documentation issue. You can add background tasks to your response as well, see the reference docs here.
So you can do something like
It's not advertised in the section about background tasks though, which it should be. I guess that's be something that should be covered in a tutorial section on background tasks, what do you think?