Skip to main content

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

Release files for django-workers 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-workers 0.2.0
File Size Uploaded
django-workers-0.2.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-workers 0.2.0
File Interpreter ABI Platform
django_workers-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / django-workers-0.2.0.tar.gz

Download URL django-workers-0.2.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ef575b64a69b533c9d5b00570e38d8483b7c53a166e0adf0f46c279dcd3b006b
BLAKE2b-256 checksum
How to use checksums
223cb71880877ee33cea97f89ae8c6088b0bbaf9a0e05cfa3d2625abcf207d0f
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / django_workers-0.2.0-py3-none-any.whl

Download URL django_workers-0.2.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72bc08a7fb7606e4f318d3e56ecbf7dde6d36a0f62d997972b08b91bf64695ab
BLAKE2b-256 checksum
How to use checksums
a9a4cfb2af45e0f07206e07a94b381034c8697640ae210b410e3d77f3e09e390
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.3

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page