Beginner's guide - LavinMQ and Celery for efficient task management

In today’s software world, handling tasks efficiently is key. But how can we manage tasks without slowing down our applications? Meet LavinMQ and Celery – two tools that can help. If you’re curious about improving your app’s performance, this guide is for you.

What is LavinMQ?

Before we jump in, let’s talk about LavinMQ. It’s a messaging system that allows different parts of your app to communicate with each other. Think of it like passing notes between friends – your app’s different sections can send messages to each other through LavinMQ. This communication helps your app handle tasks more efficiently and keeps things running smoothly.

What is Celery?

Now, let’s introduce Celery. Celery is a powerful tool for executing tasks in the background of your app. Imagine if you could assign tasks like sending emails or processing data to a separate part of your app – that’s what Celery does. It’s like having a team of helpers who take care of time-consuming jobs while your app remains responsive and fast for users.

Why Does Celery Need a Message Broker Backend like LavinMQ?

You might be wondering: why does Celery need something called a “message broker” to work? Think of the message broker as the middleman between your app and Celery. When you give Celery a task, it’s like telling your helper what needs to be done. The message broker helps Celery keep track of tasks, ensure they’re done in the right order, and manage things even if your app has many tasks to handle.

Celery uses a message broker to ensure everything runs smoothly and nothing gets overloaded. It’s like having an organized to-do list for your app’s tasks.

Setting up Celery with LavinMQ

In this simple guide, we’ll explore using LavinMQ and Celery to manage tasks. We’ll keep things easy to understand, focusing on the basics and helping you get started from learning the basics to setting things up and completing tasks faster.

Setting up LavinMQ with Celery locally involves a few steps, including installing LavinMQ, configuring Celery, defining tasks, and running the Celery worker. Here’s a step-by-step guide:

  1. Install LavinMQ: First, you need to install LavinMQ on your local machine. You can download and install it from the official website, or you can set it up in the cloud using CloudAMQP.

  2. Install Celery: Open a terminal or command prompt and install Celery using pip:

pip install celery
  1. Create a Celery Configuration: Create a file named celeryconfig.py in your project directory. This file will contain the configuration settings for Celery:
# celeryconfig.py

# Broker settings (LavinMQ)

# Replace BROKER_URL with your LavinMQ server's URL
# Example : 'amqp://guest:guest@localhost:5672//'
# Default password and username for lavinmq user: guest, pass: guest

BROKER_URL = 'lavinmq://<username>:<password>@<lavinmq_host>:<lavinmq_port>/<virtual_host>'  

# Result backend (Optional, if you want to store task results)
# result_backend = 'rpc://

# Recommended settings for local LavinMQ
broker_pool_limit = 1
broker_heartbeat = None
broker_connection_timeout = 30
result_backend = None
event_queue_expires = 60
worker_prefetch_multiplier = 1
worker_concurrency = 4  # Adjust based on your system's capabilities

More Broker Settings

Default: "amqp://"

The default broker URL. This must be a URL in the form of:

transport://userid:password@hostname:port/virtual_host

Only the scheme part ´transport://´ is required; the rest is optional and defaults to the specific transports default values. The transport part is the broker implementation, and the default is amqp, (uses librabbitmq if installed or falls back to pyamqp). The scheme can also be a fully qualified path to your transport implementation:

broker_url = 'proj.transports.MyTransport://localhost'

More than one broker URL of the same transport can also be specified. The broker URLs can be passed in as a single string that’s semicolon delimited:

broker_url = 'transport://userid:password@hostname:port//;transport://userid:password@hostname:port//'

Or as a list:

broker_url = [
    'transport://userid:password@localhost:port//',
    'transport://userid:password@hostname:port//'
]
  1. Create a Celery Instance: In your Python code, create an instance of the Celery application using the configuration from celeryconfig.py:
from celery import Celery

app = Celery('myapp')
app.config_from_object('celeryconfig')
  1. Define Celery Tasks: Define the tasks you want to execute asynchronously using Celery. For example:
@app.task
def add_numbers(x, y):
    return x + y
  1. Launch the Celery Worker: Start a Celery worker to process tasks. Open a terminal, navigate to your project directory, and execute:
celery -A your_module_name worker --loglevel=info

Replace your_module_name with the actual name of the Python module containing the Celery instance and task definitions.

  1. Dispatch Tasks for Execution:

Send tasks for asynchronous execution in your Python code:

from your_module_name import add_numbers

result = add_numbers.delay(5, 3)
print("Task ID:", result.id)
  1. Monitoring with Celery Flower: You can use Celery Flower to monitor and manage your Celery tasks locally. Install Flower using:
pip install flower

Start Flower by running:

celery -A your_module_name flower

Access the Flower web interface by opening your browser and going to http://localhost:5555.

Setting up LavinMQ with Celery locally enables you to process asynchronous tasks efficiently in your Python application. By following these steps, you can harness the capabilities of Celery and a local LavinMQ server for message queuing.

Managed LavinMQ instance on CloudAMQP

Beyond the replication and Streams features,we discussed, LavinMQ has been built with performance and ease of use in mind - we’ve benchmarked a throughput of around 1,000,000 messages/sec.

Good news is, you don’t have to install LavinMQ to test it out:

Asking for help and giving us feedback

We’d be happy to get your thoughts on what could be improved in this piece and answer your questions around using LavinMQ as well. We encourage you to join our Slack channel and talk to us there.