Fly.ioTutorial
Python Async Workers on Fly Machines
Summary
A Python web app needed to build and install libraries without blocking HTTP requests or risking backend failures when resource-heavy builds overlapped. The initial backend implementation sometimes ran for more than 30 minutes and could kill the backend, while always-on Celery workers left capacity idle. The proposed proof of concept stores job parameters in Redis, starts a Fly Machine through the Machines API with Redis key environment variables, executes a Python function, and writes the result back. A status endpoint polls the Machine state and returns PENDING until completion, after which cleanup deletes the Machine and Redis entries. The design pays for worker resources only during jobs, but the example requires JSON-serializable arguments and results and is presented as a simple implementation rather than a full job system.
Context
The application’s library builds could exceed 30 minutes and consume enough resources to kill the backend when requests overlapped. Running Celery workers continuously would also leave resources idle and incur costs for infrequent jobs.
Approach / What changed
The implementation stores task parameters and results in Redis, calls the Fly Machines API to launch a worker Machine with task-specific environment variables, executes the requested Python function, polls Machine state through a status endpoint, and deletes the Machine and Redis data after retrieval.
Takeaways
Fly Machines are started only when a background job is requested, avoiding the cost of keeping dedicated workers available during idle periods.
The worker imports a module and function named in Redis, runs the supplied args and kwargs, and stores a JSON result with a SUCCESS status.
The proof of concept limits job arguments and results to JSON-serializable values and uses a separate Docker image for the worker.
Source and freshness
- Source
- fly.io
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.