Skip to main content

Simple background tasks for Django

Project description

Django Workers

A simple background task worker that uses your Django database and admin for management. This project is meant for small to medium scale uses. If you need something more, check out Celery.

Install

Download the package

pip install django-workers

and add it to your Django installed apps

INSTALLED_APPS = [
    # ...
    'workers',
    # ...
]

Basics

Create a tasks.py file in the Django app you'd like to have tasks in. These tasks will automatically become available thanks to autodiscovery.

from workers import task

@task()
def say_hello(name):
    print('Howdy', name)

Calling tasks

Tasks become simple Python callables. Calling them inserts that task to your Django database and waits for a worker to pick it up.

say_hello('Foo')  # Sent to background automatically

Running the workers

Now boot-up your workers to crunch some data. Workers poll the Database for new tasks they should work on.

python manage.py runworkers

Scheduled tasks

Sometimes you want to run a specific task every X seconds or at a later date. Thats what scheduled tasks are for.

Repeating scheduled tasks

Tasks specified with a schedule in seconds will repeat.

from workers import task

@task(schedule=10)
def do_something():
    print('I run every 10 seconds')

@task(schedule=60*5)
def do_something_later():
    print('I run every 5 minutes')

@task(schedule=60*60*8)
def do_something_even_later():
    print('I run every 8 hours')

Date scheduled tasks

Tasks can be scheduled to run once at a later date by passing a _schedule=<datetime> argument when the task is called.

from datetime import datetime, timedelta
from workers import task

trial_end_date = datetime.utcnow() + timedelta(days=14)

@task()
def trial_ending():
    send_email('Your trial is ending!')

# Specifying the `schedule` argument will tell the worker when this task should run
trial_ending(_schedule=trial_end_date)

Settings

You can optionally override these settings in your Django settings.py file:

  • WORKERS_SLEEP (default 5) - Wait in seconds before checking for tasks, if none were found
  • WORKERS_PURGE (default 1,000) - How many recent task logs to keep in the admin

TODO (not working)

  • WORKERS_TIMEOUT (default 30) - Seconds a task can run before its killed
  • WORKERS_RETRY (default 3) - Number of retries before giving up
  • WORKERS_CONCURRENCY (default 1) - Number of worker processes to run

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-workers-0.2.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

django_workers-0.2.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file django-workers-0.2.0.tar.gz.

File metadata

  • Download URL: django-workers-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for django-workers-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ef575b64a69b533c9d5b00570e38d8483b7c53a166e0adf0f46c279dcd3b006b
MD5 e2d7826eca52e0c07f95a0bbc012cf14
BLAKE2b-256 223cb71880877ee33cea97f89ae8c6088b0bbaf9a0e05cfa3d2625abcf207d0f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_workers-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for django_workers-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72bc08a7fb7606e4f318d3e56ecbf7dde6d36a0f62d997972b08b91bf64695ab
MD5 c6816f3a5968faf07ab7658a0814c537
BLAKE2b-256 a9a4cfb2af45e0f07206e07a94b381034c8697640ae210b410e3d77f3e09e390

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