Skip to content

Background tasks

Perform long-running or blocking tasks in the background.

Info

This feature uses Dramatiq and Periodiq

Creating tasks

Tasks are python functions decorated with @app.actor.

In app/tasks.py:

from hyperflask.factory import app

@app.actor
def fetch_url(url):
    # ...

Queuing tasks

Tasks can be queued for execution from anywhere:

from app.tasks import fetch_url
fetch_url.send("http://...")

Scheduled tasks (cron jobs)

from hyperflask.factory import app
from hyperflask import cron

@app.actor(periodiq=cron("1 * * * *"))
def do_something_every_day(url):
    # ...

Brokers

The component queueing and dispatching tasks is called a "broker". Dramatiq supports multiple brokers: Rabbitmq, Redis, Amazon SQS, Postgresql.

The default broker used in Hyperflask uses sqlite. It does not require any kind of setup but will not scale past 1 server. If you want to offload your background processing to a second server, use redis or postgresql.