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, repeatedly, but not all the time.
bgt comes to the rescue with:
-
A service that runs your code in a loop and 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, that's where to look.
Background services are not a worker queue. Common use cases include:
- Periodic cleanup duties for expired caches or sessions.
- Refreshing in-memory caches or configuration.
- Flushing buffered metrics or events.
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
return False # nothing left to do: wait for the next wakeup
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
Added
- Initial public release.
It is the supervision layer from pgbg to allow using it without a PostgreSQL database.
The Prometheus metrics and the structlog logger are named
bgtinstead ofpgbg.
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. ↩
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bgt-26.1.0.tar.gz.
File metadata
- Download URL: bgt-26.1.0.tar.gz
- Upload date:
- Size: 100.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33032d188ba9964965b0fb79aa9375a441709a8d1677d327cd9ac79ee33ed0d0
|
|
| MD5 |
3af3f9e54a223284a22e3ba6c104a661
|
|
| BLAKE2b-256 |
2a64bb1b2ea7b71e2d9dee7657c694d4347d228416905cf4d3593a126f7fd46d
|
Provenance
The following attestation bundles were made for bgt-26.1.0.tar.gz:
Publisher:
pypi-package.yml on hynek/bgt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bgt-26.1.0.tar.gz -
Subject digest:
33032d188ba9964965b0fb79aa9375a441709a8d1677d327cd9ac79ee33ed0d0 - Sigstore transparency entry: 2713376319
- Sigstore integration time:
-
Permalink:
hynek/bgt@4c5f5c187a3de5c27a69e2e8c4dfc1f524889868 -
Branch / Tag:
refs/tags/26.1.0 - Owner: https://github.com/hynek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@4c5f5c187a3de5c27a69e2e8c4dfc1f524889868 -
Trigger Event:
release
-
Statement type:
File details
Details for the file bgt-26.1.0-py3-none-any.whl.
File metadata
- Download URL: bgt-26.1.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b8e8abe2e8b4efa8ad94a3f897ae9eb04c57c5c91679b93efc5e33e2165c521
|
|
| MD5 |
5614b71b1c64e47d73236ef9a85fb082
|
|
| BLAKE2b-256 |
18717b6f10fa55bc7750844b41b6da26697144b599d38f7cabc249cc7d49332c
|
Provenance
The following attestation bundles were made for bgt-26.1.0-py3-none-any.whl:
Publisher:
pypi-package.yml on hynek/bgt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bgt-26.1.0-py3-none-any.whl -
Subject digest:
3b8e8abe2e8b4efa8ad94a3f897ae9eb04c57c5c91679b93efc5e33e2165c521 - Sigstore transparency entry: 2713376749
- Sigstore integration time:
-
Permalink:
hynek/bgt@4c5f5c187a3de5c27a69e2e8c4dfc1f524889868 -
Branch / Tag:
refs/tags/26.1.0 - Owner: https://github.com/hynek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@4c5f5c187a3de5c27a69e2e8c4dfc1f524889868 -
Trigger Event:
release
-
Statement type: