Skip to content

Getting Started

Installation

bgt is available on PyPI under the bgt name:

$ uv pip install bgt

Background threads

In its simplest configuration, bgt allows you to start a thread that runs a function at a fixed interval in the background. Since real-world software crashes eventually, bgt is designed around failure recovery and does its best to keep everything running, even if your function raises an exception.

So, the following script starts a SupervisedService (a background thread) and runs for 10 seconds or until you interrupt it. It runs a loop that calls the do_work function every 2 seconds (ignore the call to as_work_factory() for a second). A single call of do_work is a work unit and the process that runs bgt threads is a worker.

In this case, there’s a 25% chance for the function to crash and a 50% chance to return True:

indie_thread.py
import random
import time

import structlog

import bgt


logger = structlog.get_logger()


def _toss_coin() -> bool:
    return random.choice([True, False])


def do_work() -> bool:
    if _toss_coin() and _toss_coin():
        raise RuntimeError("oh no I've crashed!")

    logger.info("did some work!")

    return _toss_coin()


def main() -> None:
    svc = bgt.SupervisedService.start(
        bgt.as_work_factory(do_work),
        name="example-thread",
        wakeup=bgt.IntervalOnlyWakeup(),  # only wake up on intervals
        interval=2,  # which are 2 seconds long
    )

    try:
        time.sleep(10)
    except KeyboardInterrupt:
        logger.info("shutting down")

    svc.stop()


if __name__ == "__main__":
    main()

You should see occasional crashes, but also multiple “did some work!” in quick succession.

This demonstrates two features:

  1. Crashes are reported, but the bgt-provided background thread with the loop is kept alive.
  2. If do_work returns True, it’s run again immediately.

It’s easy to get excited by fault-tolerance, but the second feature only becomes important once your work units have to stay short, for example for prompt shutdowns (see thread cancellation).

Tip

You might have seen loop constructions around threading.Event like this:

while not stop_event.is_set():
    do_work()

You can get the same behavior by writing a do_work() that always returns True. bgt checks for shutdown between calls, so your work unit does not need to manage a stop event.

Waiting for a wakeup is a strictly optional (but very useful) feature.

Crash better

We can do a little better still: since bgt is about failure recovery, it tries to encourage you to write crash-only software.

And that’s why the default shape of work is not a callable, but a factory of context managers. The previous example used as_work_factory() to adapt a plain function to it, but if you write a function that returns a context manager, that context manager is entered at the start of a loop run – and after each crash.

This gives you the ability to “microreboot” your loop on failures. That simplifies the logic a lot, since you don’t have to write error-prone recovery code.

Same example, except with an init and cleanup:

indie_with_init.py
import random
import time

from collections.abc import Generator
from contextlib import contextmanager

import structlog

import bgt

from bgt.typing import DoWork


logger = structlog.get_logger()


def _toss_coin() -> bool:
    return random.choice([True, False])


def do_work() -> bool:
    if _toss_coin() and _toss_coin():
        raise RuntimeError("oh no I've crashed!")

    logger.info("did some work!")

    return _toss_coin()


@contextmanager
def make_work() -> Generator[DoWork]:
    logger.info("work init!")
    try:
        yield do_work
    finally:
        logger.info("work cleanup!")


def main() -> None:
    svc = bgt.SupervisedService.start(
        make_work,
        name="example-thread",
        wakeup=bgt.IntervalOnlyWakeup(),  # only wake up on intervals
        interval=2,  # which are 2 seconds
    )

    try:
        time.sleep(10)
    except KeyboardInterrupt:
        logger.info("shutting down")

    svc.stop()


if __name__ == "__main__":
    main()

Now, you can see a “work init!” at the start of each loop run and a “work cleanup!” after each crash and on application exit. Your loop can have its own independent lifecycle.


And that concludes our quick tour of bgt! Keep reading our topical guides to learn more details of how it works and if there’s a proper noun overflow, don’t hesitate to check our glossary.

If you want your threads to wake up on PostgreSQL events, or want only one of your processes to do the work at a time, pgbg builds on bgt and its tutorial picks up where this one ends.