Skip to main content

Backgrounder

Backgrounder

🚀 Run background tasks in async mode in any application. 🚀

Test Suite Package version Supported Python versions


Documentation: https://backgrounder.dymmond.com 📚

Source Code: https://github.com/dymmond/backgrounder

The official supported version is always the latest released.


Motivation

Running background tasks nowadays is a must and not a nice to have and some of the the greatest frameworks out there implement this functionality as part of the ASGI reference.

Esmerald, FastAPI and Starlette by design implement the background tasks for you and those are great functionailties to have but what if you want to use background tasks without any of these frameworks? What if you simply want to have something that simply runs background tasks regardless of the framework you are using if you are using one at all*?

Well, this is where backgrounder comes to play and help you.

Compatibility

This package is 100% compatible with any ASGI framework that implements or wants to implement background tasks, which means you can even use it with Esmerald, FastAPI or Starlette without using the native version of each framework but also allows to run this inside anything else like Django for example.

Due to the nature of the package, this is only available from Python 3.10+.

How to use it

This package is actually very simple to use it, really, there is no rocket science since the library handles a lot of the magic for you.

For the purposes of these examples, we will be using Esmerald since it belongs to the same ecosystem but feel free to use with anything you want.

Using the backgrounder instead of default from the framework

As mentioned before, usually the ASGI frameworks like Esmerald come with a default background task option but let us assume you want to use backgrounder instead how it would look like.

from esmerald.responses import Response, get

from backgrounder.tasks import Task, Tasks


def set_values(values_to_add) -> None:
    for value in values_to_add:
        values.add(value)


tasks = Tasks(
    [
        Task(set_values, ["a", "b", "c", "h"]),
        Task(set_values, values_to_add=["d", "e", "f", "g"]),
    ],
    as_group=True,
)


@get(background=tasks)
async def home() -> Response:
    return Response("Task started", media_type="text/plain")

Simple, right? Internally Esmerald or any other framework will handle the process of the background tasks for you and instead of using the native library, we simply pass the Tasks object from the backgrounder and it should be it.

Using as independent async object in any application

Well, here it is where the things become interesting. What if you want to run the tasks outside of the response of a framework? Or if you want to run without a framework at all?

Well you can use the Task directly. Something like this

from backgrounder import Task

async def send_notification(email: str):
    """
    Sends an email notification to a given email
    """
    ...


task = Task(send_notification, "user@example.com")
await task()

This will make sure your task will always run in async mode and therefore, not blocking and taking advantage of the asynchronous functionality from Python.

You can also define blocking functions. Backgrounder will make sure it will always run them in async for you.

from backgrounder import Task

def send_notification(email: str):
    """
    Sends an email notification to a given email
    """
    ...


task = Task(send_notification, "user@example.com")
await task()

This can be particularly useful if you want to implement some asynchronous functionality in your applications without using a whole ASGI framework for it, for example, using Django, although now also supporting async natively, you might want to run some background tasks there that basically do not exist natively (by the time of the writting).

The decorator

Backgrounder also offers a decorator for you to use which can be extremely useful if you don't want to use the Task object directly, providing a cleaner version out of the box.

from backgrounder import background

@background
def send_notification(email: str):
    """
    Sends an email notification to a given email
    """
    ...


send_notification("user@example.com")

As simple as this, this will automatically execute the task in the background for you with all the async magic being provided.

Release files for backgrounder 0.2.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 backgrounder 0.2.0
File Size Uploaded
backgrounder-0.2.0.tar.gz 8.4 kB Details

Built distribution (wheel)

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

Total release size: 18.2 kB

Release files / backgrounder-0.2.0.tar.gz

Download URL backgrounder-0.2.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c8ea25b305e29181ec99a54e38fbaade35f07a0b4f320f9ffe8e0e4657537bc5
BLAKE2b-256 checksum
How to use checksums
e60d76dfac31426d3923f21d8b5823895885749244bcc1c55edacbca67db159d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.18

Release files / backgrounder-0.2.0-py3-none-any.whl

Download URL backgrounder-0.2.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15f560faec7eb26426c873a9c08d3a46272272793e4243bb435ad0c0228e204a
BLAKE2b-256 checksum
How to use checksums
4ece216530ff9f986c2e2f57106dab9d3cb24aeeb4380703b464d0866ad4ab56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.18

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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