Huey Task Queue
Huey is a lightweight, redis-backed task queue for Python applications. It supports thread, process, or greenlet workers, scheduling, retries, and result storage. The current version is 2.6.0, and it maintains an active release cadence with regular updates and compatibility fixes.
Warnings
- gotcha The `SIGNAL_ENQUEUED` signal, introduced in 2.5.3, runs in the *calling process* (i.e., your main application), not the separate consumer process. Operations within this signal handler directly impact your application's request/response cycle.
- gotcha When using `greenlet` workers, `huey` expects `gevent.monkey.patch_all()` to have been called *before* the `Huey` instance is created. Failure to do so (or a missing `gevent` dependency) will result in a warning and potential concurrency issues or unexpected behavior.
- gotcha Older Huey versions may have compatibility issues with newer Python releases. Specifically, versions `<2.5.1` may fail on Python 3.12+ due to `datetime.utcnow()` deprecation, and versions `<2.5.4` may have issues with multiprocessing start methods on Python 3.14+.
- breaking Huey versions older than 2.4.3 are not compatible with `redis-py` 4.0.0+ due to significant API changes in `redis-py`. Attempting to use older Huey versions with `redis-py>=4.0.0` will result in runtime errors.
Install
-
pip install huey -
pip install huey[redis]
Imports
- Huey
from huey import Huey
- RedisHuey
from huey import RedisHuey
- crontab
from huey import crontab
Quickstart
import os
from huey import RedisHuey, crontab
# Configure Huey with Redis, using an environment variable for host
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
huey = RedisHuey(host=REDIS_HOST)
# Define a simple task
@huey.task()
def add_numbers(a, b):
print(f"Adding {a} and {b}...")
return a + b
# Define a periodic task (runs every minute)
@huey.periodic_task(crontab(minute='*/1'))
def say_hello_every_minute():
print("Hello from a periodic task!")
if __name__ == '__main__':
# Enqueue tasks
print("Enqueuing tasks...")
result1 = add_numbers(10, 20)
result2 = add_numbers.schedule(args=(5, 5), delay=10)
print(f"Task 1 enqueued. Result ID: {result1.id}")
print(f"Task 2 scheduled. Result ID: {result2.id}")
# To run the consumer in a separate terminal:
# huey_consumer.py my_app.huey
# (assuming this code is in a file named my_app.py)
# To get a result (blocking):
# print(f"Result of task 1: {result1.get(blocking=True, timeout=5)}")