本文我们来分享一个python的轻型的任务队列程序,他可以让python的分布式任务huey实现异步化任务,感兴趣的朋友可以看看。
一个轻型的任务队列,功能和相关的broker没有celery强大,重在轻型,而且代码读起来也比较的简单。
关于huey的介绍: (比celery轻型,比mrq、rq要好用 !)
a lightweight alternative.
written in python
no deps outside stdlib, except redis (or roll your own backend)
support for django
supports:
multi-threaded task execution
scheduled execution at a given time
periodic execution, like a crontab
retrying tasks that fail
task result storage
安装:
代码如下 | |||||||||
Installing
|
代码如下 | |
from huey import RedisHuey, crontab huey = RedisHuey('my-app', host='redis.myapp.com') @huey.task() def add_numbers(a, b): return a b @huey.periodic_task(crontab(minute='0', hour='3')) def nightly_backup(): sync_all_data() |
juey作为woker的时候,一些cli参数。
常用的是:
-l 关于日志文件的执行 。
-w workers的数目,-w的数值大了,肯定是增加任务的处理能力
-p --periodic 启动huey worker的时候,他会从tasks.py里面找到 需要crontab的任务,会派出几个线程专门处理这些事情。
-n 不启动关于crontab里面的预周期执行,只有你触发的时候,才会执行周期星期的任务。
--threads 意思你懂的。
1
代码如下 | |||||||||
# 原文:
|
代码如下 | |
# config.py from huey import Huey from huey.backends.redis_backend import RedisBlockingQueue queue = RedisBlockingQueue('test-queue', host='localhost', port=6379) huey = Huey(queue) |
然后就是关于任务的,也就是你想让谁到任务队列这个圈子里面,和celey、rq,mrq一样,都是用tasks.py表示的。
代码如下 | |||||
from config import huey # import the huey we instantiated in config.py
|
代码如下 | |
main.py from config import huey # import our "huey" object from tasks import count_beans # import our task if __name__ == '__main__': beans = raw_input('How many beans? ') count_beans(int(beans)) print 'Enqueued job to count %s beans' % beans Ensure you have Redis running locally Ensure you have installed huey Start the consumer: huey_consumer.py main.huey (notice this is “main.huey” and not “config.huey”). Run the main program: python main.py |
再来一个真正去执行的 。 main.py 相当于生产者,tasks.py相当于消费者的关系。 main.py负责喂数据。
代码如下 | |||||||||
main.py from config import huey # import our "huey" object from tasks import count_beans # import our task if __name__ == '__main__':
count_beans(int(beans))
|
代码如下 | |
from huey import Huey from huey.backends.redis_backend import RedisBlockingQueue from huey.backends.redis_backend import RedisDataStore # ADD THIS LINE queue = RedisBlockingQueue('test-queue', host='localhost', port=6379) result_store = RedisDataStore('results', host='localhost', port=6379) # ADDED huey = Huey(queue, result_store=result_store) # ADDED result store |
代码如下 | |
>>> from main import count_beans
>>> res = count_beans(100)
>>> res # what is "res" ?
|
huey也是支持celey的延迟执行和crontab的功能 。 这些功能很是重要,可以自定义的优先级或者不用再借助linux本身的crontab。
用法很简单,多加一个delay的时间就行了,看了下huey的源码,他默认是立马执行的。当然还是要看你的线程是否都是待执行的状态了。
代码如下 | |||||
>>> import datetime
>>> res |
代码如下 | |
# tasks.py from datetime import datetime from config import huey @huey.task(retries=3, retry_delay=10) def try_thrice(): print 'trying....%s' % datetime.now() raise Exception('nope') |
再来一个重试retry的介绍,huey也是有retry,这个很是实用的东西。 如果大家有看到我的上面文章关于celery重试机制的介绍,应该也能明白huey是个怎么个回事了。 是的,他其实也是在tasks里具体函数的前面做了装饰器,装饰器里面有个func try 异常重试的逻辑 。 大家懂的。
代码如下 | |||||
# tasks.py
from datetime import datetime
from config import huey |
代码如下 | |||||
# count some beans
res = count_beans(10000000)
|
代码如下 | |
from config import huey from tasks import count_beans if __name__ == '__main__': beans = raw_input('How many beans? ') count_beans(int(beans)) print('Enqueued job to count %s beans' % beans) |
任务.py
代码如下 | |||||||||||||
随机导入
从 Huey 导入 crontab
def count_beans(num):
return '计数 %s beans' % num |
代码如下 | |
#!/bin/bash 回声“休伊消费者” 回声“-------------” echo "在另一个终端中,运行 'python main.py'" echo "使用 Ctrl C 停止消费者" PYTHONPATH=.:$PYTHONPATH python ../../huey/bin/huey_consumer.py main.huey --threads=2 => |
代码如下 | |
[xiaorui@devops /tmp ]$ git 克隆 https://github.com/coleifer/huey.git 克隆成“huey”... 远程:计数对象:1423,完成。 远程:压缩对象:100% (9/9),完成。 接收对象:34% (497/1423), 388.00 KiB | 29.00 KiB/秒 KiB/秒 接收对象:34% (498/1423), 628.00 KiB | 22.00 KiB/秒 远程:总计 1423(增量 0),重用 0(增量 0) 接收对象:100% (1423/1423),2.24 MiB | 29.00 KiB/s,完成。 解决增量:100% (729/729),完成。 检查连接...完成。 [xiaorui@devops /tmp ]$cdhuey/examples/simple [xiaorui@devops 简单(大师)]$ ll 共 40 个 -rw-r--r-- 1 小锐轮 79B 9 8 08:49 自述文件 -rw-r--r-- 1 小睿轮 0B 9 8 08:49 __init__.py -rw-r--r-- 1 小睿轮 56B 9 8 08:49 config.py -rwxr-xr-x 1 小睿轮 227B 9 8 08:49 cons.sh -rw-r--r-- 1 小睿轮 205B 9 8 08:49 main.py -rw-r--r-- 1 小睿轮 607B 9 8 08:49tasks.py [xiaorui@devops简单(主)]$ |