1

I have a route to accept request from user and return data to the user. There are the step

  1. User send request to my app
  2. Read data from database
  3. Update my database
  4. Return data to user

I want step 3 and step 4 above happen at the same time so the user will not be waiting for the update operation. How can I make this?

toblKr
  • 141
  • 2
  • 13

1 Answers1

4

The thing you might be looking is called background task and luckily, FastAPI has an awesome documentation about how to implement that. Here you have!

But be careful, your title may be implying something different from your real question. The async/await will keep you away from blocking your server, but with background tasks you can make your user wait less time for their response. Saying that, if you're going to send a response without knowing anything else, remember to send a 202 status code (Accepted).

Marcelo Trylesinski
  • 634
  • 1
  • 5
  • 13