Celery with Flask: Asynchronous Tasks in Web Applications 🎯

beginner
14 min

Celery with Flask: Asynchronous Tasks in Web Applications 🎯

Introduction 📝

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.

What is Celery? 📝

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.

Why Use Celery with Flask? 📝

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.

Installing Celery and Flask 📝

Before we dive into the details, let's make sure you have the necessary dependencies installed.

bash
pip install flask celery

Creating a Basic Flask Application 📝

Let's start by creating a simple Flask application that will serve as the foundation for our asynchronous tasks.

python
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' if __name__ == '__main__': app.run(debug=True)

Adding Celery to the Mix 📝

To add Celery to our Flask application, we'll first need to create a Celery instance.

python
from celery import Celery app = Celery('tasks', broker='amqp://guest@localhost//') @app.task def add(x, y): return x + y

In 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.

Integrating Celery with Flask 📝

Now that we have our Celery application and tasks, let's integrate them with our Flask application.

python
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.

Testing Our Application 📝

Let's test our application by running it and sending a POST request to the /add route with some numbers.

bash
curl -X POST -d 'x=3&y=5' http://localhost:5000/add

You should see a response similar to the following:

json
{"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:

bash
celery -A wsgi worker --loglevel=info

Conclusion 📝

In 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.

Quiz 💡

Quick Quiz
Question 1 of 1

What is Celery used for in Flask applications?