Using Flask with Celery on Ubuntu
Celery Environment
- backend: redis
- broker: redis
Installation
- redis server
$ apt-get install redis-server - python library
$ pip install redis $ pip install celery
Project Structure
project
├── __init__.py
├── web.py
├── work
├── __init__.py
├── worker.py
├── templates
├── index.html
- worker.py
from celery import Celery
cel = Celery('work',
backend='redis://127.0.0.1:6379/',
broker='redis://127.0.0.1:6379/')
@cel.task
def expensive_function(x, y):
return x + y
- web.py
from flask import Flask, render_template
from work.worker import expensive_function
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
result = expensive_function.delay(3, 3)
return render_template("index.html",
val=result.wait())
if __name__ == '__main__':
app.run()
- index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<h1>The value from celery's expensive function is {{ val }}</h1>
</body>
</html>
Execution
If redis is not working, running redis.
$ redis-serverCheck redis is working.
$ redis-cli ping PONGStart redis in interactive mode, you can know which ip and port is redis running.
$ redis-cli redis 127.0.0.1:6379>Go to project directory
$ cd /your_project_path/projectRun celery worker
$ celery -A work.worker worker --loglevel=info --concurrency=2--loglevel=info: print log on terminal--concurrency=2: specify the number of threads
Run flask web app
$ python web.pyNow, you can visit your web site, the web index will print
The value from celery's expensive function is 6.And, the celery log will print
Received task successfully and return the 6 of computing resultat same time.