celery任务调度模块

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

Celery是Python开发的分布式任务调度模块,Celery本身不含消息服务,它使用第三方消息服务来传递任务,目前,Celery支持的消息服务有RabbitMQ、Redis甚至是数据库。

安装celery

pip install Celery

当使用redis时需要再安装celery-with-redis

celery的tasks脚本编写

例子:

import time

from celery import Celery

#指定redis的地址和库

broker='redis://localhost:6379/0'

backend='redis://localhost:6379/1'

#指定名字

celery = Celery('tasks', broker=broker, backend=backend)

@celery.task

def add(x, y):

    return x+y

def sendmail(mail):

    print('sending mail to %s...' % mail['to'])

    time.sleep(2.0)

    print('mail sent.')

启动task任务

#需要将task任务部署到linux服务器上,并将此任务运行,如果为运行此任务,则无法向redis传入值,也无法监控返回状态,执行命令如下

celery -A tasks worker -l info

调用celery接口

例子:

from celery_task import add,sendmail

import time

a = add.delay(10, 20)

print (a)

print (type(a))

time.sleep(1)

#查看celery返回的结果

print (a.result)

#查看celery的返回状态

print (a.status)

#指定超时时间为10秒

print (a.get(timeout=10))

#是否处理完成

print (a.ready())

#调用sendmail

sendmail.delay(dict(to='celery@python.org'))


celery多任务处理

celeryconfig.py

#celery多任务的配置文件,需要放到服务器中

from kombu import Exchange,Queue

#需要按照celery格式

BORKER_URL = "redis://192.168.1.1:6379/1"

CELERY_RESULT_BACKEND = "redis://192.168.1.1/2"

CELERY_QUEUES = {

    Queue("default", Exchange("default"), routing_key=("default")),

    Queue("for_task_A", Exchange("for_task_A"), routing_key=("for_task_A")),

    Queue("for_task_B", Exchange("for_task_B"), routing_key=("for_task_B"))

}

CELERY_ROUTES = {

    "celery_practice.taskA":{"queue":"for_task_A", "routing_key":"for_task_A"},

    "celery_practice.taskB":{"queue":"for_task_B", "routing_key":"for_task_B"}

}

#celery定时任务

CELERY_TIMEZONE = 'UTC'

CELERYBEAT_SCHEDULE = {

    'taskA_schedule':{

        'task':'celery_practice.taskA',

        #暂停时间

        'schedule':20,

        'args':(5, 6)

    },

    'taskB_schedule':{

        'task':'celery_practice.taskB',

        'schedule':50,

        'args':(100, 200, 300)

    }

    'add_schedule': {

        'task': 'celery_practice.add',

        'schedule': 10,

        'args': (10, 20)

    }

}

celery_practice.py

#多任务内容,也需要放到服务器上运行

from celery import Celery

app =  Celery()

#同celery多任务配置文件脚本名

app.config_from_object("celeryconfig")

@app.task

def taskA(x, y):

    return x*y

@app.task

def taskB(x, y, z):

    return x+y+z

@app.task

def add(x, y):

    return x+y

celery_practice1.py

#具体客户端程序

from celery_practice import *

r1 = taskA.delay(10, 20)

print (r1.result)

r2 = taskB.delay(1, 2, 3)

print (r2.result)

r3 = add.delay(5, 10)

print (r3.status)

linux服务器上需要运行内容

celery -A celery_practice worker -l info -n workerA.%h -Q for_task_A

celery -A celery_practice worker -l info -n workerB.%h -Q for_task_B

celery -A celery_practice beat



本文转自 粗粮面包 51CTO博客,原文链接:http://blog.51cto.com/culiangmianbao/2052267,如需转载请自行联系原作者

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
8天前
|
监控 NoSQL 测试技术
python使用Flask,Redis和Celery的异步任务
python使用Flask,Redis和Celery的异步任务
19 0
|
8月前
|
消息中间件 开发框架 NoSQL
celery--介绍
celery--介绍
|
8月前
|
缓存 NoSQL Redis
celery--实现异步任务
celery--实现异步任务
|
8月前
celery--定时任务
celery--定时任务
|
消息中间件 监控 NoSQL
python3.7+Tornado5.1.1+Celery3.1+Rabbitmq3.7.16实现异步队列任务
在之前的一篇文章中提到了用[Django+Celery+Redis实现了异步任务队列](https://v3u.cn/a_id_54),只不过消息中间件使用了redis,redis作为消息中间件可谓是差强人意,功能和性能上都不如Rabbitmq,所以本次使用tornado框架结合celery,同时消息中间件使用Rabbitmq来实现异步发邮件,并且使用flower来监控任务队列。
python3.7+Tornado5.1.1+Celery3.1+Rabbitmq3.7.16实现异步队列任务
|
NoSQL API 调度
Celery初探
Celery初探
199 0
|
NoSQL 调度 Redis
Django celery6.4
Django celery6.4
115 0
|
Python
django celery 异步执行任务遇到的坑
django celery 异步执行任务遇到的坑
Django查看celery任务结果
异步任务执行的时候对于用户来说总是会出现再次尝试,第二次查看结果的时候如果能查到任务结果如何那就可以减少好多不必要的查询和重试。
|
消息中间件 存储 监控
Celery基本使用
Celery 是一款非常简单、灵活、可靠的分布式系统,可用于处理大量消息、调度异步任务,并且提供了一整套操作此系统的一系列工具。
3004 0
Celery基本使用