Skip to content

Commit 66a2566

Browse files
committed
add celeries
1 parent 9be10d8 commit 66a2566

13 files changed

Lines changed: 169 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ ENV/
8888

8989
# Rope project settings
9090
.ropeproject
91+
# celery-beat
92+
*.bak
93+
*.dat
94+
*.dir

celeries/proj/__init__.py

Whitespace-only changes.

celeries/proj/celery.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding=utf-8
2+
from __future__ import absolute_import
3+
from celery import Celery
4+
5+
app = Celery('proj', include=['proj.tasks'])
6+
app.config_from_object('proj.celeryconfig')
7+
8+
9+
if __name__ == '__main__':
10+
app.start()

celeries/proj/celeryconfig.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# coding=utf-8
2+
BROKER_URL = 'amqp://localhost' # RabbitMQ 作为消息代理
3+
CELERY_RESULT_BACKEND = 'redis://localhost' # Redis 作为结果存储
4+
CELERY_TASK_SERIALIZER = 'msgpack'
5+
# 任务序列化和反序列化格式为 msgpack, 别忘了安装 msgpack-python
6+
CELERY_RESULT_SERIALIZER = 'json' # 结果存储序列化格式为 json
7+
CELERY_ACCEPT_CONTENT = ['msgpack', 'json'] # 任务接受格式类型

celeries/proj/tasks.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# coding=utf-8
2+
from __future__ import absolute_import
3+
4+
from .celery import app
5+
6+
from celery.utils.log import get_task_logger
7+
8+
logger = get_task_logger(__name__)
9+
10+
11+
@app.task
12+
def add(x, y):
13+
return x + y
14+
15+
16+
@app.task
17+
def mul(x, y):
18+
return x * y
19+
20+
21+
@app.task(bind=True)
22+
def div(self, x, y):
23+
logger.info(
24+
'''
25+
Executing task : {0.id}
26+
task.args : {0.args!r}
27+
task.kwargs : {0.kwargs!r}
28+
'''.format(self.request)
29+
)
30+
try:
31+
res = x / y
32+
except ZeroDivisionError as e:
33+
raise self.retry(exc=e, countdown=3, max_retries=3)
34+
else:
35+
return res

celeries/projb/__init__.py

Whitespace-only changes.

celeries/projb/celery.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding=utf-8
2+
from __future__ import absolute_import
3+
from celery import Celery
4+
5+
app = Celery('projb', include=['projb.tasks'])
6+
app.config_from_object('projb.celeryconfig')
7+
8+
9+
if __name__ == '__main__':
10+
app.start()

celeries/projb/celeryconfig.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# coding=utf-8
2+
from kombu import Queue
3+
4+
BROKER_URL = 'amqp://localhost' # RabbitMQ 作为消息代理
5+
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0' # Redis 作为结果存储
6+
CELERY_TASK_SERIALIZER = 'msgpack'
7+
# 任务序列化和反序列化格式为 msgpack, 别忘了安装 msgpack-python
8+
CELERY_RESULT_SERIALIZER = 'json' # 结果存储序列化格式为 json
9+
CELERY_ACCEPT_CONTENT = ['msgpack', 'json'] # 任务接受格式类型
10+
11+
CELERY_QUEUES = {
12+
Queue('foo', routing_key='task.#'),
13+
Queue('feed_task', routing_key='*.feed'),
14+
}
15+
CELERY_DEFAULT_QUEUE = 'foo'
16+
17+
CELERY_DEFAULT_EXCHANGE = 'tasks'
18+
19+
CELERY_DEFAULT_EXCHANGE_TYPE = 'topic'
20+
21+
CELERY_DEFAULT_ROUTING_KEY = 'task.foooooo'
22+
23+
CELERY_ROUTES = {
24+
'projb.tasks.mul': {
25+
'queue': 'feed_task',
26+
'routing_key': 'mul.feed',
27+
},
28+
}

celeries/projb/tasks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# coding=utf-8
2+
from __future__ import absolute_import
3+
4+
from .celery import app
5+
6+
7+
@app.task
8+
def add(x, y):
9+
return x + y
10+
11+
12+
@app.task
13+
def mul(x, y):
14+
return x * y

celeries/projc/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)