-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to run background tasks within a custom APIRoute? #3133
Comments
I don't get it , write what you would like to have even if the code is not valide |
I want to be able to use background_tasks as fastapi recommends (path parameter). However in order to use background_tasks without being a path param the docs say to instantiate it yourself via starlette docs(https://www.starlette.io/background/). Here's a code sample of what I would like to happen. class AuditedRoute(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
# this will throw "TypeError: custom_route_handler() missing 1 required positional argument: 'background_tasks' "
async def custom_route_handler(request: Request, background_tasks: BackgroundTasks) -> Response:
try:
response: Response = await original_route_handler(request)
except RequestValidationError as exc:
body = await request.body()
detail = {"errors": exc.errors(), "body": body.decode()}
raise HTTPException(status_code=422, detail=detail)
background_tasks.add_task(send_message, 'foo bar')
return response
return custom_route_handler I'm specifically tying to avoid having to import startlette.response and returning those responses. I want to be able to return the response that comes out of |
Why do you need a custom route handler? Wouldn't you be able to do most of this with dependencies or a middleware? I think it can help if you comment what's your intention, what you are trying to solve, what is the main problem, before thinking about the specific implementation. I think there's a high chance there's a better way to achieve what you need, but I'm not sure yet what you need.
|
hey! so I will preface this with I no longer need this solution. but for whats its worth, let me try again. my question was: given a custom api route class such as https://fastapi.tiangolo.com/advanced/custom-request-and-route/#custom-apiroute-class-in-a-router, how would I inject the background_tasks object into the the requirements from what i remember.. sorry its been a while 😬
|
closing since I think this is resolved via middleware |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
I'm trying to work out how to add background tasks into my custom route handler. I read the part in the docs about using starlette response object when instantiating backround task on my own but am hoping its possible to do it the fastapi native way.
The text was updated successfully, but these errors were encountered: