标签:imp 定时 support res 开发 过期 结果 rand error
前言:
Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, 举几个实例场景中可用的例子:
Celery有以下优点:
Celery基本工作流程图:
1、 Celery安装使用
Celery需要在linux的环境下运行:
# 安装 [root@localhost celerys]# pip3 install celery # 进入python import无异常表示安装成功 [root@localhost celerys]# python3 >>> import celery
Celery的默认broker是RabbitMQ, 仅需配置一行就可以
broker_url = ‘amqp://guest:guest@localhost:5672//‘
使用Redis做broker也可以
broker_url = ‘redis://localhost:6379/0‘ #redis://:password@hostname:port/db_number
2、简单使用
创建一个任务文件就叫tasks.py:
from celery import Celery import time app = Celery(‘cly‘, # 任意 broker=‘redis://192.168.1.166:6379/0‘, # 中间件 backend=‘redis://localhost‘) # 数据存储 @app.task def add(x,y): time.sleep(10) print("running...",x,y) return x+y
启动Celery Worker来开始监听并执行任务:
# 加入环境变量 [root@localhost ~]# PATH=$PATH:/usr/local/python3.5/bin/ # 启动一个worker [root@localhost celerys]# celery -A tasks worker --loglevel=info
调用任务:
[root@localhost celerys]# python3 Python 3.5.2 (default, Jul 7 2017, 23:36:01) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from tasks import add # import add >>> add.delay(4,6) # 执行函数 <AsyncResult: 4b5a8ab6-693c-4ce5-b779-305cfcdf70cd> # 返回taskid >>> result = add.delay(4,6) # 执行函数 >>> result.get() # 同步获取结果,一直等待 10 >>> result.get(timeout=1) # 设置超时时间,过期错误异常 Traceback (most recent call last): --strip-- celery.exceptions.TimeoutError: The operation timed out. >>> result = add.delay(4,‘a‘) # 执行错误命令 >>> result.get() # get后获取到错误信息,触发异常 Traceback (most recent call last): --strip-- celery.backends.base.TypeError: unsupported operand type(s) for +: ‘int‘ and ‘str‘ >>> result = add.delay(4,‘a‘) >>> result.get(propagate=False) # propagate=False 不触发异常,获取错误信息 TypeError("unsupported operand type(s) for +: ‘int‘ and ‘str‘",) >>> result.traceback # 获取具体错误信息 log打印用 ‘Traceback (most recent call last):\n File "/usr/local/python3.5/lib/python3.5/site-packages/celery/app/trace.py", line 367, in trace_task\n R = retval = fun(*args, **kwargs)\n File "/usr/local/python3.5/lib/python3.5/site-packages/celery/app/trace.py", line 622, in __protected_call__\n return self.run(*args, **kwargs)\n File "/data/celerys/tasks.py", line 12, in add\n return x+y\nTypeError: unsupported operand type(s) for +: \‘int\‘ and \‘str\‘\n‘
此时worker端收到的信息:
[2017-07-08 03:12:22,565: WARNING/PoolWorker-1] running... # 获取到任务 [2017-07-08 03:12:22,565: WARNING/PoolWorker-1] 4 [2017-07-08 03:12:22,565: WARNING/PoolWorker-1] 6 # 任务执行完毕数据存储到backend端 [2017-07-08 03:12:22,567: INFO/PoolWorker-1] Task tasks.add[683e395e-48b9-4d32-b3bb-1492c62af393] succeeded in 10.01260852499945s: 10
查看worker(即192.168.1.166)端数据:
[root@localhost redis-3.0.6]# src/redis-cli 127.0.0.1:6379> keys * 1) "_kombu.binding.celeryev" 2) "unacked_mutex" 3) "_kombu.binding.celery.pidbox" 4) "_kombu.binding.celery"
执行完后,backend端的数据:
[root@localhost redis-3.0.6]# src/redis-cli # 程序get后,数据未被删除 127.0.0.1:6379> keys * 1) "celery-task-meta-683e395e-48b9-4d32-b3bb-1492c62af393"
Python开发【模块】:Celery 分布式异步消息任务队列
标签:imp 定时 support res 开发 过期 结果 rand error
原文地址:http://www.cnblogs.com/lianzhilei/p/7133295.html