Giter Site home page Giter Site logo

dennisstritzke / django-tasks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from realorangeone/django-tasks

1.0 0.0 0.0 160 KB

A reference implementation and backport of background workers and tasks in Django

Home Page: https://pypi.org/project/django-tasks/

License: BSD 3-Clause "New" or "Revised" License

Python 99.52% Just 0.48%

django-tasks's Introduction

Django Tasks

CI PyPI PyPI - Python Version PyPI - Status PyPI - License

A reference implementation and backport of background workers and tasks in Django, as defined in DEP 0014.

Warning: This code is not intended to be installed, let alone deployed to production!

Installation

pip install django-tasks

Usage

Note: This documentation is still work-in-progress. Further details can also be found on the DEP. The tests are also a good exhaustive reference.

Settings

The first step is to configure a backend. This connects the tasks to whatever is going to execute them.

If omitted, the following configuration is used:

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.immediate.ImmediateBackend"
    }
}

A few backends are included by default:

  • DummyBackend: Don't execute the tasks, just store them. This is especially useful for testing.
  • ImmediateBackend: Execute the task immediately in the current thread
  • DatabaseBackend: Store tasks in the database (via Django's ORM), and retrieve and execute them using the db_worker management command

Defining tasks

A task is created with the task decorator.

from django_tasks import task


@task()
def calculate_meaning_of_life() -> int:
    return 42

The task decorator accepts a few arguments to customize the task:

  • priority: The priority of the task (larger numbers are higher priority)
  • queue_name: Whether to run the task on a specific queue
  • backend: Name of the backend for this task to use (as defined in TASKS)

These attributes can also be modified at run-time with .using:

modified_task = calculate_meaning_of_life.using(priority=10)

In addition to the above attributes, run_after can be passed to specify a specific time the task should run. Both a timezone-aware datetime or timedelta may be passed.

Enqueueing tasks

To execute a task, call the enqueue method on it:

result = calculate_meaning_of_life.enqueue()

The returned TaskResult can be interrogated to query the current state of the running task, as well as its return value.

If the task takes arguments, these can be passed as-is to enqueue.

Executing tasks with the database backend

First, you'll need to add django_tasks.backends.database to INSTALLED_APPS, and run manage.py migrate.

Next, configure the database backend:

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.database.DatabaseBackend"
    }
}

Finally, you can run manage.py db_worker to run tasks as they're created. Check the --help for more options.

Sending emails

To make sending emails simpler, a backend is provided to automatically create tasks for sending emails via SMTP:

EMAIL_BACKEND = "django_tasks.mail.SMTPEmailBackend"

SMTP credentials are configured as usual.

django-tasks's People

Contributors

realorangeone avatar sobolevn avatar

Stargazers

Jasper Stritzke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.