Python: Using Flask + Celery + RabbitMQ
Celery is a pretty simple task queue that runs in the background. (asynchronous)
Using Celery, a program can respond faster while some heavy tasks are still running in the background so that you don't have to wait for a program to finish all the heavy tasks to complete, and star doing something else instead of just waiting.
For example, function like this will always take a long time to finish because of the heavy process
Barista function
* Take an order (fast)
* Payment process (fast)
* Make a special drink (slow)
* Find a person who ordered the drink (forever)
* Done
What Celery does is kind of like this
Barista function
* Take an order (fast)
* Payment process (fast)
* Done
OFFLOAD these heavy tasks in the background
* Make a special drink (slow)
* Find a person who ordered this drink (forever)
so that you don't have to wait for a barista to finish making one drink at a time.
Setup Celery
1. Install Celery
pip install celery
2. Install RabbitMQ
You can install locally, but heroku has this "free" add on you can use and don't need to worry about local settings.
https://elements.heroku.com/addons/cloudamqp
Just one click "Install CloudAMQP"
Once it is installed, grab "AMQP URL"
Flask
Once you get these two things installed, you can offload complex tasks in the background like this
@celery.task
Using decorator like this to offload a task and use "delay" to call this task
from flask import Flask
from celery import Celery
app = Flask(__name__)
# AMQP URL from Heroku 👇
app.config['CELERY_BROKER_URL'] = 'amqps://nmrwbflq:***@jellyfish.rm'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@app.route('/')
def welcome():
task = make_drink.delay()
return 'Welcome to Celery!'
@celery.task
def make_drink():
# grind beans
# prepare milk stuff
# warm up a cup
# or everything you do to make a drink
# or something else
That is it!
When you are welcomed, drink making is started in the background so you don't have to wait for drink to be done 😉