Skip to content

Unclear how to create a background task with dependencies from within the handler scope #765

Answered by provinzkraut
scott2b asked this question in Q&A
Discussion options

You must be logged in to vote

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?

Replies: 4 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@scott2b
Comment options

Answer selected by provinzkraut
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@peterschutt
Comment options

@provinzkraut
Comment options

@scott2b
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Documentation 📚 This is related to documentation
3 participants