├── .gitignore ├── LICENSE ├── README ├── app.py ├── example.py └── settings.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv/* 2 | 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Packages 9 | *.egg 10 | *.egg-info 11 | dist 12 | build 13 | eggs 14 | parts 15 | bin 16 | var 17 | sdist 18 | develop-eggs 19 | .installed.cfg 20 | lib 21 | lib64 22 | 23 | # Installer logs 24 | pip-log.txt 25 | 26 | # Unit test / coverage reports 27 | .coverage 28 | .tox 29 | nosetests.xml 30 | 31 | #Translations 32 | *.mo 33 | 34 | #Mr Developer 35 | .mr.developer.cfg 36 | 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | copyright (c) 2013 Thrisp-Hurrata 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | A simple example for Flask with Celery 2 | 3 | see: http://flask.pocoo.org/docs/patterns/celery/ 4 | 5 | In this project directory start celery: 6 | 7 | celery -A app.celery worker& 8 | 9 | then start the application: 10 | 11 | python app.py& 12 | 13 | then run the example script 14 | 15 | python example.py 16 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from os import path, environ 3 | import json 4 | from flask import Flask, Blueprint, abort, jsonify, request, session 5 | import settings 6 | from celery import Celery 7 | 8 | app = Flask(__name__) 9 | app.config.from_object(settings) 10 | 11 | def make_celery(app): 12 | celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL']) 13 | celery.conf.update(app.config) 14 | TaskBase = celery.Task 15 | class ContextTask(TaskBase): 16 | abstract = True 17 | def __call__(self, *args, **kwargs): 18 | with app.app_context(): 19 | return TaskBase.__call__(self, *args, **kwargs) 20 | celery.Task = ContextTask 21 | return celery 22 | 23 | celery = make_celery(app) 24 | 25 | @celery.task(name="tasks.add") 26 | def add(x, y): 27 | return x + y 28 | 29 | @app.route("/test") 30 | def hello_world(x=16, y=16): 31 | x = int(request.args.get("x", x)) 32 | y = int(request.args.get("y", y)) 33 | res = add.apply_async((x, y)) 34 | context = {"id": res.task_id, "x": x, "y": y} 35 | result = "add((x){}, (y){})".format(context['x'], context['y']) 36 | goto = "{}".format(context['id']) 37 | return jsonify(result=result, goto=goto) 38 | 39 | @app.route("/test/result/") 40 | def show_result(task_id): 41 | retval = add.AsyncResult(task_id).get(timeout=1.0) 42 | return repr(retval) 43 | 44 | 45 | if __name__ == "__main__": 46 | port = int(environ.get("PORT", 5000)) 47 | app.run(host='0.0.0.0', port=port, debug=True) 48 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import random 3 | 4 | for i in range(100): 5 | p = {'x':random.randrange(1,10000000), 'y':random.randrange(1,1000000000)} 6 | r = requests.get('http://localhost:5000/test', params = p) 7 | print r.json() 8 | gto = r.json()['goto'] 9 | r2 = requests.get("http://localhost:5000/test/result/{}".format(gto)) 10 | print r2.json() 11 | -------------------------------------------------------------------------------- /settings.py: -------------------------------------------------------------------------------- 1 | SECRET_KEY = 'not_a_secret' 2 | CELERY_BROKER_URL='redis://localhost:6379/0' 3 | CELERY_RESULT_BACKEND='redis://localhost:6379/0' 4 | --------------------------------------------------------------------------------