Skip to content

Light, high-load ready, handy background-task module for Django.

License

Notifications You must be signed in to change notification settings

taggart-comet/background_task

Repository files navigation

Light, high-load ready, handy background-task module for Django.

Django 3 \ Python 3 \ Mysql (can probably work with older Django and Postgres)

Scalable. Writes logs in MySQL, with rotation and handy access for displaying in an admin area. Easy to attach statistics and set triggers on queue overflow.

Use case #1:

I need to send emails to my users asynchronously.

Step #1: Write a task class:

Wherever in your code you feel is right, create a class:

from BackgroundTask.interface import BackgroundTaskInterface
from django.core.mail import BadHeaderError, send_mail


class QueueSendEmail(BackgroundTaskInterface):

    def work(self, data):

        try:
            send_mail(
                data['subject'],
                data['message'],
                'from@example.com',
                [data['email']],
                fail_silently=False,
            )
        except BadHeaderError:
            return False
        
        return True

Step #2: Add this class to settings.py:

BACKGROUND_TASK_CLASSES = [
    'mypath.QueueSendEmail'
]

Step #3: Create tables for queues and logs:

./manage.py background_task --migrate

Step #4: Launch the queue:

./manage.py background_task --activate

Step #5: Add tasks to the queue

from ApiHandler.response import ApiResponse
from django.views.decorators.http import require_POST
from mypath.email import QueueSendEmail

@require_POST
def send_email_code(request):

    # ...

    # adding task to send the email
    QueueSendEmail().add_task({
        'email': email,
        'subject': 'Login to myawesomesite.com',
        'message': 'Your login code is: %s' % code
    })

    # ...

    return ApiResponse.ok()

Lots of customizations available, well documented in interface.py

About

Light, high-load ready, handy background-task module for Django.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published