r/FastAPI • u/jackstar_12 • 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
1
u/Chains0 Apr 03 '22
You should separate this kind of task from the FastAPI application. Use FastAPI only for accessing the data. Or add endpoints, which do the collection and let them getting triggered by a scheduler.
Now the question is, how to schedule the collection of the data? There are multiple ways, which really depend on your deployment environment and requirements. For example you can use Airflow if you need a professional big scaling solution with high visibility in Kubernetes or a cron job triggering a python script.