Skip to main content

bgt

Fault-tolerant background threads for Python

POV: you want a framework-agnostic way to reliably run a plain or async 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 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. For async work units such as async def do_work() use bgt.as_async_work_factory().

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/.

Ecosystem

There are currently two packages built on top of bgt:

  • pgbg 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!

  • django-bgt adapts bgt for Django. It provides a service decorator for your work functions, using the Django ORM to orchestrate leader election and automatic failover, while remaining database agnostic.

Release Information

Added

  • bgt.as_async_work_factory() adapter for async work units that don't require setup, cleanup, or control over the event loop. #6

Full Changelog →

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.

Metadata

Release files for bgt 26.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bgt 26.3.0
File Size Uploaded Provenance Actions
bgt-26.3.0.tar.gz 114.4 kB With provenance Details for bgt-26.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bgt 26.3.0
File Interpreter ABI Platform Actions
bgt-26.3.0-py3-none-any.whl Python 3 none any Details for bgt-26.3.0-py3-none-any.whl

Total release size: 127.4 kB

Release files / bgt-26.3.0.tar.gz

Download URL bgt-26.3.0.tar.gz
Size 114.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6f75cd2ecb02273e4203b220fc9761edb1f2aa4904b1ae864bc8190b76797ca3
BLAKE2b-256 checksum
How to use checksums
3617bd750d511f8c8a13b767a0814a8080f98d3bc070d61923bee7f65f3704d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 14, 2026.

Transparency log

Release files / bgt-26.3.0-py3-none-any.whl

Download URL bgt-26.3.0-py3-none-any.whl
Size 13.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38d4aed9e0caa280e14aefc2ceeca104eb0af1e306da4e24c07a9325630fbc12
BLAKE2b-256 checksum
How to use checksums
94ba6ec2ec1ad01855b80898d8c5fc69807e45570593974dce21378aac468e67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 14, 2026.

Transparency log

Security for bgt 26.3.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bgt-26.3.0.tar.gz

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 Nov 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page