Package: backgrounder
Backgrounder🚀 Run background tasks in async mode in any application. 🚀Documentation:https://backgrounder.dymmond.com📚Source Code:https://github.com/dymmond/backgrounderThe official supported version is always the latest released.MotivationRunning 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,FastAPIandStarletteby 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 usingif you are using one at all*?Well, this is wherebackgroundercomes to play and help you.CompatibilityThis package is 100% compatible with any ASGI framework that implements or wants to implement
background tasks, which means you can even use it withEsmerald,FastAPIorStarlettewithout using the native version of each framework butalso allowsto run this inside anything else
likeDjangofor example.Due to the nature of the package, this is only available from Python 3.10+.How to use itThis 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 usingEsmeraldsince it belongs to the
same ecosystem but feel free to use with anything you want.Using the backgrounder instead of default from the frameworkAs mentioned before, usually the ASGI frameworks likeEsmeraldcome with a default background
task option but let us assume you want to usebackgrounderinstead how it would look like.fromesmerald.responsesimportResponse,getfrombackgrounder.tasksimportTask,Tasksdefset_values(values_to_add)->None:forvalueinvalues_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)asyncdefhome()->Response:returnResponse("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 theTasksobject from
thebackgrounderand it should be it.Using as independent async object in any applicationWell, 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 theTaskdirectly. Something like thisfrombackgrounderimportTaskasyncdefsend_notification(email:str):"""Sends an email notification to a given email"""...task=Task(send_notification,"user@example.com")awaittask()This will make sure your task will always run inasyncmode and therefore, not blocking and
taking advantage of the asynchronous functionality from Python.You can also defineblockingfunctions.Backgrounderwill make sure it will always run them
inasyncfor you.frombackgrounderimportTaskdefsend_notification(email:str):"""Sends an email notification to a given email"""...task=Task(send_notification,"user@example.com")awaittask()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, usingDjango, although
now also supportingasyncnatively, you might want to run some background tasks there that basically
do not exist natively (by the time of the writting).The decoratorBackgrounderalso offers a decorator for you to use which can be extremely useful if you don't
want to use theTaskobject directly, providing a cleaner version out of the box.frombackgrounderimportbackground@backgrounddefsend_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
theasyncmagic being provided.