Use Procrastinate in a Django application#

Defining tasks#

Add your tasks to tasks.py in your Django app. Inside tasks, you can use the classical ORM API or the async ORM API to access your models.

from procrastinate.contrib.django import app

@app.task
def mytask1(obj_pk):
    obj = MyModel.objects.get(pj=obj_pk)
    ...

@app.task
async def mytask2(obj_pk):
    obj = await MyModel.objects.aget(pj=obj_pk)
    ...

See Define a task for more details on how to define tasks.

Running the worker & other CLI commands#

Run the worker with the following command.

(venv) $ ./manage.py procrastinate worker

./manage.py procrastinate mostly behaves like the procrastinate command itself. The subcommand schema is not available, as Procrastinate will use Django migrations. The --app option is also not available, as Procrastinate the automatically provided app (see Configure Django & Procrastinate to work together).

Note

As a fully async connector is needed to run the worker, Procrastinate generates one for you using Psycopg (by default) or Aiopg depending on whether psycopg version 3 or aiopg is installed, and connects using the DATABASES settings. If neither library is installed, an error will be raised.

See Use the command line for more details on the CLI. If you prefer writing your own scripts, see Running custom Django scripts.

Deferring jobs#

Defer jobs from your views works as you would expect:

from myapp.tasks import mytask

def myview(request):
    ...
    mytask.defer(obj_pk=obj.pk)

async def myasyncview(request):
    ...
    await mytask.defer_async(obj_pk=obj.pk)

See Defer a job for more details on how to defer jobs.

Checking proper configuration#

You can check that Procrastinate is properly configured by running the following command:

(venv) $ ./manage.py procrastinate healthchecks
Database connection: OK
Migrations: OK
Default Django Procrastinate App: OK
Worker App: OK

See Monitor Procrastinate in a real environment for more details on healthchecks.