Skip to main content

Simple and easy to use background tasks in Django without dependencies!

Project description

Package Testing PyPI version PyPI Downloads

Introduction

Simple and easy to use background tasks in Django without dependencies!

Features

  • Easy background task creation
  • 🛤️ Multiple queue support
  • 🔄 Automatic task retrying
  • 🛠️ Well integrated with your chosen database
  • 🚫 No additional dependencies
  • 🔀 Supports both sync and async functions

Documentation

🙂 Click HERE

Instalation

pip install django_firefly_tasks

Setup

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ###############
    'django_firefly_tasks',
]

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,

    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
    },

    'loggers': {
        'django_firefly_tasks': {
            'handlers': ['console'],
            'level': 'INFO',
            'propagate': False,
        },
    },
}

Quick Start

views.py

from django.http.response import JsonResponse, Http404

from django_firefly_tasks.models import TaskModel
from django_firefly_tasks.decorators import task
from django_firefly_tasks.utils import task_as_dict


@task(queue="default", max_retries=0, retry_delay=0)
# param "queue" defines the queue in which the task will be placed
# param "max_retries" defines max retries on fail
# param "retry_delay" defines delay in seconds  between restarts
def add(i: int, j: int) -> int:
    return i + j


def task_view(request):
    """
    Example response
    ---
    {
        "id": 1,
        "func_name": "app.views.add",
        "status": "created",
        "not_before": null,
        "created": "2025-04-27T17:28:36.109Z",
        "retry_attempts": 0,
        "retry_delay": "0s",
        "max_retries": 0
    }
    """
    # pass function args to schedule method
    task = add.schedule(1, 3)
    return JsonResponse(task_as_dict(task))


def task_detail_view(request, task_id):
    """
    Example response
    ---
        4
    """
    try:
        task = TaskModel.objects.get(pk=task_id)
    except TaskModel.DoesNotExist:
        raise Http404("Task does not exist")
    # task.returned stores function returned data 
    return JsonResponse(task.returned, safe=False)

urls.py

from django.urls import path

from .views import task_view, task_detail_view

urlpatterns = [
    path('task/', task_view, name='task_view'),
    path('task/<int:task_id>', task_detail_view, name='task_detail_view'),
]

Finally, run consumer. Default queue is called "default". Consumer doesn't have auto-reload, so when tasks changed it requires manual restart.

./manage.py consume_tasks

Frequently Asked Questions

Consumer is too slow, what can I do?

Set in your settings.py CONSUMER_NAP_TIME lower value (default 0.001 aka 1000 tasks per second). You can also try to scale it horizontally by defining multiple queues and running multiple consumers for each one.

Can I run multiple consumers for the same queue?

Yes, but it is not recommended. Consumers could lock each other.

Support

If this project was useful to you, feel free to buy me a coffee ☕. It doesn't have to be from Starbucks — even a budget one is just fine ;) Every donation, no matter how small, is a sign that what I’m doing is valuable to you and worth maintaining.

paypal

Contact

If you're missing something, feel free to add your own Issue or PR, which are, of course, welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_firefly_tasks-0.2.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_firefly_tasks-0.2.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file django_firefly_tasks-0.2.0.tar.gz.

File metadata

  • Download URL: django_firefly_tasks-0.2.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for django_firefly_tasks-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e759461bbfba847e676a22599cdf19b824bfb86e0c056f671f8ced949a3e02c7
MD5 2660f2b5d7dc98d903109e3382b77964
BLAKE2b-256 6ad39dff82b26f91ede79dc268cbdd23b41f33614c2a542e70b4f4a813904062

See more details on using hashes here.

File details

Details for the file django_firefly_tasks-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_firefly_tasks-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ada9db42aa03a9da2012098e6afc90f3df3c7b61ab8c50990c70f4925b2d57c
MD5 143d98ada9466d4932a0b76c56f72e8b
BLAKE2b-256 62ec4fe3557cb3cbf508a4f376bbf19506926779cd1620dbc32142b1063bc48a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page