How to use Celery with LavinMQ

LavinMQ is the message broker for your Celery tasks. Celery handles creating and executing tasks, while LavinMQ sits between your application and Celery workers, storing task messages and delivering them to workers when they are ready. It speaks AMQP natively, uses little memory, and needs nothing more than a connection string to get started.

Celery is a Python library for running tasks in the background instead of blocking your main application. It needs a message broker to move tasks between clients and workers, and LavinMQ handles that job.

Teams use Celery with LavinMQ for things like sending order confirmations without blocking checkout, encoding video after upload, generating reports in the background, or running scheduled jobs like shipment tracking updates.

Python's py-amqp, the library Celery runs on, shows up on about 13% of CloudAMQP clusters. That number includes anyone using py-amqp directly, not just Celery users, so it's a rough signal rather than an exact Celery count.

Step 1: Install Celery

pip install celery

Step 2: Create a LavinMQ instance

Sign up for a CloudAMQP account and create a new LavinMQ instance. The free plan works fine for testing.

Once the instance is up, open its details page and copy the AMQP connection URL. It looks like this:

amqps://user:password@host/vhost

Step 3: Point Celery at LavinMQ

Set broker_url to your LavinMQ connection string. Save this as tasks.py:

from celery import Celery

app = Celery('tasks', broker='amqps://user:password@host/vhost')

@app.task
def add(x, y):
    return x + y

Celery talks to LavinMQ over standard AMQP, so no extra transport or driver is needed.

Step 4: Start a worker and send a task

celery -A tasks worker --loglevel=info

From another shell or script, send a task:

from tasks import add

result = add.delay(4, 6)
print(result.get())

The worker picks up the task from LavinMQ and returns the result.

Summary

LavinMQ is a solid default for Celery: fast to set up, low on resource use, and built on the AMQP protocol Celery already speaks.

Already running Celery on another broker? Moving to LavinMQ is usually just a matter of updating the broker_url.

Create a free LavinMQ instance and try it with your own Celery tasks.

CloudAMQP - industry leading RabbitMQ as a service

Start your managed cluster today. CloudAMQP is 100% free to try.

13,000+ users including these smart companies