Welcome to this comprehensive guide on using Celery with Flask! In this tutorial, we'll learn how to leverage the power of Celery to handle asynchronous tasks in your Flask web applications. By the end of this guide, you'll have a solid understanding of how to set up and use Celery with Flask, as well as practical examples that will help you in your own projects.
Celery is a Python-based distributed task queue based on the Message Passing Interface (MPI) standard. It allows you to perform asynchronous tasks in your applications, which can significantly improve the performance and scalability of your web applications.
Flask is a micro web framework in Python that makes it easy to build web applications. However, Flask is synchronous by default, which means that when a request comes in, the application waits for the response before moving on to the next request. This can lead to performance issues, especially when dealing with heavy computations or IO-bound tasks.
Celery can help overcome these issues by allowing you to offload time-consuming or CPU-intensive tasks to other worker processes. This way, your web application can respond quickly to user requests while the heavy lifting is being done in the background.
Before we dive into the details, let's make sure you have the necessary dependencies installed.
pip install flask celeryLet's start by creating a simple Flask application that will serve as the foundation for our asynchronous tasks.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)To add Celery to our Flask application, we'll first need to create a Celery instance.
from celery import Celery
app = Celery('tasks', broker='amqp://guest@localhost//')
@app.task
def add(x, y):
return x + yIn the code above, we've created a new module called tasks.py that contains our Celery application instance and an add task that takes two numbers as arguments and returns their sum.
Now that we have our Celery application and tasks, let's integrate them with our Flask application.
from flask import Flask, request
from celery import current_task
app = Flask(__name__)
app.config['CELERY_RESULT_BACKEND'] = 'db+sqlite:////tmp/celery.sqlite'
app.config['CELERY_TASK_RESULT_EXPIRES'] = 3600
app.config['CELERY_TIMEZONE'] = 'UTC'
app.conf.update(app.options.get('task_routes', {}))
celery_app = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery_app.conf.update(app.config)
celery_app.autodiscover_tasks(lambda: __package__)
@app.route('/add', methods=['POST'])
def add():
x = request.form['x']
y = request.form['y']
result = current_task.send(arg1=x, arg2=y)
return {'task_id': result.id, 'status': 'submitted'}
if __name__ == '__main__':
celery_app.start()
app.run(debug=True)In the code above, we've added a new route to our Flask application called /add that accepts POST requests. When a request is received, it creates a new Celery task using the add function from our tasks.py module and returns the task ID.
Let's test our application by running it and sending a POST request to the /add route with some numbers.
curl -X POST -d 'x=3&y=5' http://localhost:5000/addYou should see a response similar to the following:
{"task_id": "a2709600-c3e3-4623-a663-65d4882a8d8c", "status": "submitted"}Now, if you check the Celery task status, you'll see that the task is being processed in the background:
celery -A wsgi worker --loglevel=infoIn this tutorial, we've learned how to integrate Celery with Flask to handle asynchronous tasks in our web applications. By using Celery, we can offload time-consuming or CPU-intensive tasks to other worker processes, improving the performance and scalability of our applications.
What is Celery used for in Flask applications?