Python Celery Asynchronous Tasks with RabbitMQ Broker

Adi Ramadhan
2 min readJan 28, 2023

--

Running asynchronous task with Celery Worker and RabbitMQ. It strongly recommended for long running tasks.

Prerequisite:
RabbitMQ Server (localhost, heroku or other rabbitmq server)

Step 1: Preparation
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install celery in virtualenv: pip install celery sqlalchemy eventlet
Create App Folder: mkdir app
Go to myproject folder: cd app

Step 2: Create tasks.py file

from celery import Celery
from time import sleep

#connect to local rabbitmq and db sqlite backend
app = Celery('tasks', broker = 'amqp://localhost', backend = 'db+sqlite:///db.sqlite3')

@app.task #register task into celery app via decorator
def get_hello(name):
sleep(10) #simulation for long running task
retval = f"Hello {name}"
return retval

Step 3: Run celery worker

Open new cmd/terminal and go to virtualenv shell follow with python shell
start virtualenv: venv/Scripts/activate
Go to myproject folder: cd app
Run celery worker with following command: celery -A tasks worker — loglevel=info -P eventlet

(venv) celery -A tasks worker --loglevel=info -P eventlet
Celery Worker Run

Step 4: Send message task to queued and let celery worker run

Open new cmd/terminal and go to virtualenv shell follow with python shell
start virtualenv: venv/Scripts/activate
Go to myproject folder: cd app
Go to python shell: python

Send queued message with and return with result, python code result = get_hello.delay(‘Mark’)

(venv) python
>>> from tasks import get_hello
>>> result = get_hello.delay('Mark')
>>> result
result = get_hello.delay('Mark')
>>> result.status
<AsyncResult: f1501bdb-400f-4a71-bbe1-c6e00ffefa02>
>>> result.status
'PENDING'
>>> result.status
'SUCCESS'
>>> result.get()
'Hello Mark'

--

--