We have a project on FastAPI. It is still very lightly loaded, so after a few hours of inactivity we get an error of this kind . All sorts of recipes with pool_recycle and adding manipulations to the decorator of DB operations do not help. I decided to go around the problem - to turn off workers every hour. I sketched out the following scheme: main.py import asyncio from fastapi import FastAPI from .workers import worker_shutdown app = FastAPI() @app.on_event("startup") async def worker_lifetime(): loop = asyncio.get_event_loop() loop.create_task(worker_shutdown()) workers.py import os import signal import asyncio import random async def worker_shutdown(): pid = os.getpid() delay = 3600 + random.randint(2,60) await asyncio.sleep(delay) os.kill(pid, signal.SIGTERM) All this works as intended. The only question is, is it correct? Will it cause any unexpected problems? There was also an idea to move worker_shutdown to a separate thread using threading. Continue reading...