Python + Flask + Celery: Initial configuration

Hi, everyone!

This is the first post of the series on how to use asynchronous tasks in a Python web app using celery. For those who still haven't heard of it, Celery is, other than a plant, a task queue implementation for Python applications. It is very easy and simple to use. We are going to do now, the basic configuration to have a project with flask and celery running

1.Start a message broker

First thing to have in mind is that we need a message broker up and running. Celery supports RabbitMQ, Redis and Amazon SQS. In this post, we are going to use Redis as the message broker. After we certify that redis is running:

redis-cli ping
>>PONG

Let's go to the next step.

2.Install Celery and Flask

If you have pip, it is very easy and straightfoward to install them:

pip install celery
pip install flask

3.Initial Flask configuration file

Let's create a file named app.py to start our flask app:

project_name/app/src/app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World'

Let's checkout if it worked!

$ export FLASK_APP=app/src/app.py
$ python -m flask run
 * Serving Flask app "app/src/app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/

4.Basic Celery configuration

Now we are going to create a file named "worker.py" to add our celery configuration:

project_name/app/src/worker.py

from celery import Celery
import os

#get the message broker url
redis_url = os.environ.get("REDIS_URL", 'redis://localhost:6379')

celery = Celery(__name__, backend="%s/0" % redis_url, broker="%s/1" % redis_url)

def create_app(app):
    celery.conf.update(app.config)

And we are going to import and use create app to add celery configurations on the app/src/app.py file:

from flask import Flask
from .worker import create_app #add this line

app = Flask(__name__)

create_app(app)  #add this line too

@app.route('/')
def hello_world():
    return 'Hello World'

To see if it worked, run the following command from your project root:

$celery -A app.src.worker worker -l info

You should see something like this:

[2021-04-22 18:41:21,309: INFO/MainProcess] Connected to redis://localhost:6379/1
[2021-04-22 18:41:21,317: INFO/MainProcess] mingle: searching for neighbors
[2021-04-22 18:41:22,337: INFO/MainProcess] mingle: all alone
[2021-04-22 18:41:22,365: INFO/MainProcess] celery@DESKTOP-GHQPR21 ready.

Now both celery and flask are up and running and we can start playing!

You can checkout the result of the post here :

In the next post, we are going to create a few tasks!