Skip to content

FastAPI BackgroundTask problem #152363

Answered by melody413
melody715 asked this question in General
Discussion options

You must be logged in to vote

Background tasks, as the name suggests, are tasks that are going to run in the background after returning a response. Hence, you can't raise an Exception and expect the client to receive some kind of response. If you are just looking to catch any Exception occuring inside the background task, you can simply use the try-except block to catch any Exception and handle it as desired. For example:

def test_func(a, b): try: # some background task logic here... raise <some_exception> except Exception as e: print('Something went wrong') # use print(e.detail)to print out the Exception's details

If you would like to log any exceptions being raised in the task (instead of just printing them out), y…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by melody715
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
1 reply
@melody715
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants