Huey Task Queue

2.6.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up Huey with a Redis backend, define a basic task, and a periodic task. It also shows how to enqueue tasks and retrieve their results. Remember to run a consumer process separately using `huey_consumer.py` to process tasks.

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)}")

view raw JSON →