bgt
Fault-tolerant background threads for Python
POV: you want a framework-agnostic way to reliably run a plain1 function or method in the background, continuously or with pauses between work units.
bgt comes to the rescue with:
-
A service that runs your code in a loop and optionally waits between work units. It wakes up on a fixed time interval, or as soon as something wakes it.
-
A supervisor that runs that loop in a background thread. If your code crashes, the supervisor restarts the loop after an exponential backoff. Write crash-only code, bgt takes care of the rest.
-
Thorough instrumentation via structlog and Prometheus.
-
Framework and platform independence.
bgt is the engine underneath pgbg, which adds PostgreSQL LISTEN / NOTIFY-driven wakeups and leader election with automatic failover on top.
If your services should wake up on database events, or only one process at a time should do the work, check it out!
bgt is not a job queue like Celery or RQ. Common use cases include:
- Periodic cleanup duties for expired caches or sessions.
- Flushing buffered metrics or events.
- Processing a continuous stream of data in bounded batches.
Here's a service that runs a work unit every two seconds and survives its own crashes:
import bgt
def do_work() -> bool:
... # one bounded work unit; if it crashes, it doesn't crash the loop
return False # False = wait for next wakeup; True = run again immediately
with bgt.SupervisedService.start(
bgt.as_work_factory(do_work),
name="example",
wakeup=bgt.IntervalOnlyWakeup(),
interval=2,
):
... # do_work runs in the background until we leave this block
Return True from your work unit to be run again immediately.
This keeps work units short, which makes shutdowns prompt.
Check out our step-by-step tutorial to get an instant feel for the features!
Installation
The package is available on PyPI under the bgt name:
$ uv pip install bgt
Documentation
Full documentation lives at https://bgt.hynek.me/.
Release Information
Changed
- Just docs!
Credits
bgt is written by Hynek Schlawack and distributed under the terms of the MIT license.
The development is kindly supported by my employer Variomedia AG and all my fabulous GitHub Sponsors.
-
As in: not
async. ↩
Metadata
Release files for bgt 26.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bgt-26.2.0.tar.gz | 101.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bgt-26.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 113.2 kB
Release files / bgt-26.2.0.tar.gz
| Download URL | bgt-26.2.0.tar.gz |
|---|---|
| Size | 101.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
de2a379b1f67db3d1d4a072b023074c26a7a7222a3ad147d18812a3555e06849
|
|
BLAKE2b-256 checksum How to use checksums |
9b19e179eb5269ec960e823dbbc3bdd0bbf661a52cc57fbbc93d0999289d3578
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.
Transparency logRelease files / bgt-26.2.0-py3-none-any.whl
| Download URL | bgt-26.2.0-py3-none-any.whl |
|---|---|
| Size | 12.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3dd88c0c2d6dd26140491eb58ee69daf43a2056b76e3ff819a24cf16850202a9
|
|
BLAKE2b-256 checksum How to use checksums |
f61bf24dfc0d36499e04987b655fe22cc50aa85ce0b6657a3dda5235dd83937e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.
Transparency log