Skip to content

Commit d8b00da

Browse files
committed
revised to accomodate celery development version and changed to simple example
1 parent 8047c60 commit d8b00da

File tree

9 files changed

+46
-56
lines changed

9 files changed

+46
-56
lines changed

README

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
A simple example for the latest version of Celery (2.6.0rc4 as of this example)
2+
3+
4+
In your project directory start celery:
5+
6+
python tasks.py worker --loglevel=debug
7+
8+
then start the application:
9+
10+
python app.py
11+
12+
<optional> then run the testing script
13+
14+
python outside_test.py

app.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,12 @@
33
from flask import Flask, Blueprint, abort, jsonify, request, session
44
import settings
55
from modules import myapi
6-
from flask.ext.celery import Celery
7-
6+
from celery import Celery
7+
from tasks import add
88

99
app = Flask(__name__)
1010
app.config.from_object(settings)
1111

12-
13-
app.register_blueprint(myapi.blueprint)
14-
15-
def register_api(view, endpoint, url, pk ='id', pk_type='int'):
16-
view_func = view.as_view(endpoint)
17-
app.add_url_rule(url, defaults={pk: None}, view_func=view_func, methods=['GET',])
18-
app.add_url_rule(url, view_func=view_func, methods=['POST',])
19-
app.add_url_rule('{0}<{1}:{2}>'.format(url, pk_type, pk), view_func=view_func,
20-
methods=['GET', 'PUT', 'DELETE'])
21-
22-
register_api(myapi.MyAPI, 'my_api', '/my_api/', 'resource_tag', pk_type='string')
23-
24-
celery = Celery(app)
25-
26-
@celery.task(name="myapp.add")
27-
def add(x, y):
28-
return x + y
29-
3012
@app.route("/test")
3113
def hello_world(x=16, y=16):
3214
x = int(request.args.get("x", x))

celeryconfig.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#celery
2+
#BROKER_URL = 'redis://localhost:6379/0'
3+
#CELERY_RESULT_BACKEND = "redis"
4+
#CELERY_REDIS_HOST = "localhost"
5+
#CELERY_REDIS_PORT = 6379
6+
#CELERY_REDIS_DB = 0

manager.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

modules/__init__.py

Whitespace-only changes.

modules/myapi.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

outside_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#testing basic celery setup
2+
import requests
3+
import random
4+
5+
for i in range(100):
6+
p = {'x':random.randrange(1,10000000), 'y':random.randrange(1,1000000000)}
7+
r = requests.get('http://localhost:5000/test', params = p)
8+
print r.json
9+
r2 = requests.get("http://localhost:5000/test/result/{}".format(r.json['goto']) )
10+
print r2.json
11+

settings.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
SECRET_KEY = 'hey_now_this_is_no_secret'
2-
3-
#celery
4-
BROKER_URL = 'redis://localhost:6379/0'
5-
CELERY_RESULT_BACKEND = "redis"
6-
CELERY_REDIS_HOST = "localhost"
7-
CELERY_REDIS_PORT = 6379
8-
CELERY_REDIS_DB = 0
1+
SECRET_KEY = 'not_a_secret'

tasks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from celery import Celery
2+
3+
celery = Celery("tasks",
4+
broker='redis://localhost:6379/0',
5+
backend='redis')
6+
7+
@celery.task(name="tasks.add")
8+
def add(x, y):
9+
return x + y
10+
11+
if __name__ == "__main__":
12+
celery.start()

0 commit comments

Comments
 (0)