Python之Celery(一)
来源:互联网 发布:反贪风暴 知乎 编辑:程序博客网 时间:2024/05/16 08:49
Python之Celery
1.celery简介
celery 是一个python开发的异步分布式的任务调度模块,目前只支持rebbimg,redis,数据库等
安装celery
pip install celery
pip install redis
2.celery小例子
实例一:
#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time : 2017/12/25 14:39
# @Author : hantong
# @File : celery_1.py
# celery 是一个python开发的异步分布式的任务调度模块,目前只支持rebbimg,redis,数据库等
# 安装celery
# pip install celery
# pip install redis
from celery import Celery
broker ="redis://10.3.1.126:6379/5"
backend = "redis://10.3.1.126:6379/6"
app = Celery("celery_1",broker=broker,backend=backend)
@app.task
def add(x,y):
return x+y
此实例执行不返回结果
实例二:
#!/usr/bin/env python# -*- coding:utf8 -*-# @Time : 2017/12/25 15:11# @Author : hantong# @File : celery_2.pyimport timefrom celery_1 import adda = add.delay(10,20)print(a)print(type(a))time.sleep(1)print(a.result)print(a.status)print(a.get(timeout=3))print(a.ready())执行结果:
0bfb5366-dec4-4aeb-afa0-621d654a4ba4
<class 'celery.result.AsyncResult'>
30
SUCCESS
30
True
阅读全文