Celery 分布式框架 学习

时间:2022-07-22
本文章向大家介绍Celery 分布式框架 学习 ,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、概要

官网: http://www.celeryproject.org/

官方文档: http://docs.celeryproject.org/en/latest/index.html

一个AMQP: http://abhishek-tiwari.com/post/amqp-rabbitmq-and-celery-a-visual-guide-for-dummies

其他的文章,一般都太老了。还是直接看官方文档吧。

有问题还是直接去Google或直接去StackOverflow吧,百度真心不靠谱!!!


二、简单DEMO

tasks.py

# coding:utf-8

from celery import Celery

# backend = 'db+mysql://root:@localhost/celery'
backend = 'amqp'
broker = 'amqp://guest@localhost//'
app = Celery('tasks', backend=backend, broker=broker)


@app.task
def add(x, y):
    return x + y

添加参数直接以 worker 形式运行即可,便形成了一个 worker。

# celery -A tasks worker -l debug
# debug 调试模式,会输出更多调试信息
celery -A tasks worker -l info

#### 此时,在windows平台(win8 x64)出现问题

可以打开多个终端,执行上述命令。已形成 “分布式”多个 worker。

然后,添加异步任务。

D:celery_projectcel>python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from tasks import add
>>> x = add.delay(3,9)
>>> x.status
'PENDING'
>>> x.ready()
False
>>>

#### 就在此时,很容易看到, worker 终端,显示任务已经完成,但是 主机 status状态:PENDING ready状态:False

然后各种百度,更换 backend -> amqp -> db+mysql 依旧无效,然后 在 StackOverflow 上找到了答案,吼吼~~~ 好兴奋的感觉

Problem: Celery 'Getting Started' not able to retrieve results; always pending

链接: https://stackoverflow.com/questions/25495613/celery-getting-started-not-able-to-retrieve-results-always-pending

解决方法就是,添加运行参数: celery -A tasks worker -l debug --pool=solo

D:celery_projectcel>python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from tasks import add
>>> x = add.delay(3,6)
>>> x.status
'SUCCESS'
>>> x.ready()
True
>>> x.result
9

至此,Celery调度,基本使用基本可以,欢迎留言交流探讨~~~~好吧,其实欢迎指教~~~