01.Celery 简介、安装和HelloWrold

简介

Celery是一个开源的分布式系统,支持任务队列实时处理,也支持定时任务。
Celery4.0是支持Python2.7的最后一个版本,后续版本需要使用Python3.5+.
Celery不支持Windows系统.
Celery是使用Python编写,但是他的协议可以被任何语言实现,除了Python之外还有支持node.js和PHP的版本.

安装

pip install celery 或者pip install celery[redis]
其他安装方法查看官网

简单小例子

#----tasks.py-----
from celery import Celery
app = Celery(__name__, broker="redis://localhost")#使用redis作为broker 需要安装redis

@app.task
def hello():
    return "HelloWrold"

使用celery -A tasks worker -l info运行该程序
-A是app
tasks是tasks.py
worker 该程序已worker运行
-l info是log level 是info

不出意外你的程序应该已经启动了.那么怎么调用一下让它工作呢?
我们打开一个python解释器.

import tasks
tasks.hello.delay()#按下回车后,我们会看到刚才Celery的打印信息,能看出执行了我们的hello方法.

参考文档
http://docs.celeryproject.org/en/latest/index.html

猜你喜欢

转载自blog.csdn.net/a540366413/article/details/73276599