r/FastAPI • • Apr 03 '22

Question Manage background activity

Hey guys.

I am creating an FastAPI application that is collecting user data in the background 24/7 and saving it to the DB.

I was wondering:

How would I go about implementing something like this in a multi-worker environemnt? Because I don't want the data collection task to run in every process, but I also need to access the data throughout the API.

3 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/jackstar_12 Apr 04 '22

In this case I thought about the collector as a seperate process that communicates with the uvicorn instances created by gunicorn. It's not really scheduled as it runs 24/7, holding multiple websockets.

The FastAPI instances don't control the collector. They only tell him when e.g. a new user is added, so the collector can include that one too.

1

u/Chains0 Apr 06 '22

I think I slowly understand what’s going on in your setup. You have something like a user service created with FastAPI. This one uses as well a websocket connection for the frontend? Then if a add_user notification is received, he sends a notification via websocket connection to your collector service, which saves the user into the database. Is that correct?

But I guess I missed something, as this setup does not require background tasks, just notifications. Do you want to aggregate the data before writing to the db?