Unlock the Power of Asynchronous Tasks in Your Flask App

Are you tired of waiting for long-running tasks to complete in your Flask application? Do you want to know the secret to offloading these tasks and freeing up your app to handle more requests? Look no further! In this article, we’ll explore how to use Celery to run tasks asynchronously, giving your users a seamless experience.

The Problem with Long-Running Tasks

We’ve all been there – you’re working on a feature that requires calling a third-party API, sending an email, or generating a PDF report. These tasks can take seconds, if not minutes, to complete, blocking the request/response cycle and leaving your users waiting. But what if you could offload these tasks to separate worker threads, freeing up your app to handle more requests?

Introducing Celery

Celery is a distributed task queue that allows you to run tasks asynchronously. It communicates via messages, using a broker to mediate between clients and workers. By setting up Celery with a Redis server, you can offload long-running tasks and let your app focus on what it does best.

Getting Started with Celery and Redis

To get started, you’ll need Python 3.6+, Virtualenv v20+, and intermediate knowledge of Python and Flask. Don’t worry if you’re new to Celery – we’ll walk you through the setup process step by step.

First, download the starter project and set it up using the following commands:


git clone https://github.com/username/flask-celery-starter.git
cd flask-celery-starter
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt

Next, let’s add a route that will contain a button to trigger a mock long-running task. Open app.py and add the following code:

“`
from flask import Flask, render_template
import time

app = Flask(name)

@app.route(‘/tasks’)
def tasks():
return render_template(‘tasks.html’)

@app.route(‘/longrunningtask’)
def longrunningtask():
time.sleep(15)
return ‘Task completed!’
“`

Create a new file named tasks.html in the templates directory:

“`



Tasks

Tasks



“`

Running Tasks Asynchronously with Celery

Now, let’s see how we can use Celery to run this task in the background. Create a new file called celery_utils.py:

“`
from celery import Celery

def getceleryapp_instance():
app = Celery(‘tasks’, broker=’redis://localhost:6379/0′)
return app
“`

Next, let’s define a long-running task using Celery. Add the following code to app.py:

“`
from celeryutils import getceleryappinstance

celeryapp = getceleryappinstance()

@celeryapp.task
def sending
emailwithcelery():
time.sleep(15)
return ‘Email sent!’
“`

Finally, define a route to trigger this function:


@app.route('/long_running_task_celery')
def long_running_task_celery():
sending_email_with_celery.delay()
return 'Task triggered!'

Putting it All Together

To see Celery in action, start the Flask server and Celery worker in separate terminal windows:


python app.py
celery -A celery_utils worker

Open http://127.0.0.1:5000/tasks in your browser and click on the “Trigger Long-Running Task with Celery” button. You’ll see that the page instantly redirects, and the task is executed in the background by Celery.

Recap

That’s it! You now know how to set up and run long-running tasks with Celery in your Flask web application. To recap, to run a function as a Celery task, you need to:

  • Import the instance of Celery app in your file
  • Add the decorator @celery.task on top of the function definition
  • Run the function using the function_name.delay() method

By offloading long-running tasks to Celery, you can create a more responsive and scalable Flask application that delights your users.

Leave a Reply

Your email address will not be published. Required fields are marked *