
Background tasks for FastAPI apps that need more than BackgroundTasks and less than Celery.
If you have used APScheduler inside a FastAPI app, you have probably met one of these problems:
- A task runs too long and the user wants to cancel it, but there is no clean way to signal the worker while it runs.
- A background job must send progress to a websocket, and you write
run_coroutine_threadsafe code by hand to get back onto the main loop.
- You want a job id on every log line of one run, and you pass that id through every call by hand.
- You have a complete async pipeline to run in the background, and you wrap it in
asyncio.run to give it to a scheduler that accepts sync code only.
quiv was built inside Trailarr, a FastAPI app that left APScheduler for these reasons. It is a scheduler for one process, backed by a thread pool, with an optional pool of processes for the work that needs a CPU of its own or a way to be ended by force. It has three things built in: cooperative cancellation through stop_event, progress callbacks that run on the main loop through progress_hook, and a job id for tracing through job_id.
quiv does not replace Celery. If you need workers in several processes, a queue that survives a restart, or work spread over machines, use Celery or arq. Use quiv when the work belongs inside your own process, where those tools would be far more than you need.
Supports Python 3.10 through 3.14.
Install
With uv
uv add quiv
With pip
pip install quiv
Quick example
A full FastAPI integration — lifespan startup, an endpoint that schedules work, and progress streaming back to the main loop:
from contextlib import asynccontextmanager
from fastapi import FastAPI
from quiv import Quiv
# Create the Quiv scheduler
scheduler = Quiv(timezone="UTC")
# Wire it up in FastAPI's lifespan so that it starts and dies with your app
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
scheduler.start()
yield
# Shutdown
scheduler.shutdown()
# Create FastAPI app
app = FastAPI(lifespan=lifespan)
# Create a test function that we can later schedule to broadcast progress
# sync/async - doesn't matter; quiv handles them all
def ping(progress_hook=None):
for i in range(30):
# do some work
if progress_hook:
progress_hook(message="ping", progress=i, total=30)
# Now the actual progress callback function that we want to run on the main asyncio loop
async def on_progress(**payload):
# Replace with websocket broadcast, logging, metrics, etc.
print("progress", payload)
# Create the endpoint function that will schedule the task when triggered
@app.post("/start-heartbeat")
def start_heartbeat():
task_id = scheduler.add_task(
task_name="heartbeat",
func=ping,
interval=30,
progress_callback=on_progress,
)
return {"task_id": task_id}
What you actually get
Run async handlers natively, no asyncio.run wrapper