python 任务队列 huey_huey:小型多线程任务队列

Huey是一个用Python编写的轻量级任务队列,支持多进程、多线程或greenlet执行模型,可以使用Redis、SQLite或内存存储。它提供简单的API,支持定时任务、延时任务、周期性任务、任务重试、任务优先级等功能。
摘要由CSDN通过智能技术生成

a lightweight alternative.

huey is:

a task queue (2019-04-01: version 2.0 released)

written in python (2.7+, 3.4+)

clean and simple API

redis, sqlite, or in-memory storage

huey supports:

multi-process, multi-thread or greenlet task execution models

schedule tasks to execute at a given time, or after a given delay

schedule recurring tasks, like a crontab

automatically retry tasks that fail

task prioritization

task result storage

task locking

task pipelines and chains

At a glance

from huey import RedisHuey, crontab

huey = RedisHuey('my-app', host='redis.myapp.com')

@huey.task()

def add_numbers(a, b):

return a + b

@huey.task(retries=2, retry_delay=60)

def flaky_task(url):

# This task might fail, in which case it will be retried up to 2 times

# with a delay of 60s between retries.

return this_might_fail(url)

@huey.periodic_task(crontab(minute='0', hour='3'))

def nightly_backup():

sync_all_data()

Calling a task-decorated function will enqueue the function call for execution by the consumer. A special result handle is returned immediately, which can be used to fetch the result once the task is finished:

>>> from demo import add_numbers

>>> res = add_numbers(1, 2)

>>> res

>>> res()

3

Tasks can be scheduled to run in the future:

>>> res = add_numbers.schedule((2, 3), delay=10) # Will be run in ~10s.

>>> res(blocking=True) # Will block until task finishes, in ~10s.

5

For much more, check out the guide or take a look at the example code.

Running the consumer

Run the consumer with four worker processes:

$ huey_consumer.py my_app.huey -k process -w 4

To run the consumer with a single worker thread (default):

$ huey_consumer.py my_app.huey

If your work-loads are mostly IO-bound, you can run the consumer with threads or greenlets instead. Because greenlets are so lightweight, you can run quite a few of them efficiently:

$ huey_consumer.py my_app.huey -k greenlet -w 32

Storage

Huey's design and feature-set were informed by the capabilities of the Redis database. Redis is a fantastic fit for a lightweight task queueing library like Huey: it's self-contained, versatile, and can be a multi-purpose solution for other web-application tasks like caching, event publishing, analytics, rate-limiting, and more.

Although Huey was designed with Redis in mind, the storage system implements a simple API and many other tools could be used instead of Redis if that's your preference.

Huey comes with builtin support for Redis, Sqlite and in-memory storage.

Documentation

Project page

Huey is named in honor of my cat:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值