Convert Figma logo to code with AI

coleifer logohuey

a little task queue for python

5,544
382
5,544
0

Top Related Projects

26,926

Distributed Task Queue (development branch)

10,281

Simple job queues for Python

A fast and reliable background task processing library for Python 3.

Task scheduling library for Python

6,795

Real-time monitor and web admin for Celery distributed task queue

Quick Overview

Huey is a lightweight and flexible task queue for Python, designed to handle asynchronous task execution, scheduling, and result storage. It supports multiple backends, including Redis, SQLite, and in-memory storage, making it suitable for various application sizes and requirements.

Pros

  • Simple and easy to use, with a clean API
  • Supports multiple backends (Redis, SQLite, in-memory)
  • Lightweight with minimal dependencies
  • Offers features like task scheduling, retries, and result storage

Cons

  • Not as feature-rich as some larger task queue libraries
  • Limited built-in monitoring and administration tools
  • May not be suitable for extremely large-scale distributed systems
  • Documentation could be more comprehensive

Code Examples

  1. Defining and executing a task:
from huey import RedisHuey

huey = RedisHuey('my-app')

@huey.task()
def add_numbers(a, b):
    return a + b

# Enqueue the task
result = add_numbers(1, 2)
print(result.get())  # Blocks until the result is ready
  1. Scheduling a task:
from huey import crontab

@huey.periodic_task(crontab(minute='0', hour='*/3'))
def periodic_task():
    print("This task runs every 3 hours")
  1. Using task pipelines:
@huey.task()
def task_1():
    return 'result 1'

@huey.task()
def task_2(result):
    return f'result 2 based on {result}'

pipeline = task_1.s() | task_2.s()
result = pipeline()
print(result.get())  # Prints: result 2 based on result 1

Getting Started

  1. Install Huey:

    pip install huey
    
  2. Create a tasks file (e.g., tasks.py):

    from huey import RedisHuey
    
    huey = RedisHuey('my-app')
    
    @huey.task()
    def my_task():
        print("Task executed!")
    
  3. Run the Huey consumer:

    huey_consumer.py tasks.py
    
  4. In your application, import and use the tasks:

    from tasks import my_task
    
    result = my_task()
    

Competitor Comparisons

26,926

Distributed Task Queue (development branch)

Pros of Celery

  • More feature-rich and mature, with extensive documentation
  • Supports a wider range of message brokers (RabbitMQ, Redis, Amazon SQS, etc.)
  • Offers more advanced task routing and workflow management capabilities

Cons of Celery

  • More complex setup and configuration process
  • Heavier resource usage, especially for smaller projects
  • Steeper learning curve for beginners

Code Comparison

Celery task definition:

from celery import Celery

app = Celery('tasks', broker='redis://localhost:6379')

@app.task
def add(x, y):
    return x + y

Huey task definition:

from huey import RedisHuey

huey = RedisHuey()

@huey.task()
def add(x, y):
    return x + y

Both Celery and Huey are Python task queue libraries, but they differ in complexity and feature set. Celery is more suitable for large-scale applications with complex task management needs, while Huey is simpler and more lightweight, making it ideal for smaller projects or those new to task queues. The code comparison shows that both libraries have similar syntax for defining tasks, but Celery requires more initial setup.

10,281

Simple job queues for Python

Pros of RQ

  • More extensive documentation and community support
  • Built-in support for job prioritization and result expiration
  • Easier integration with Flask and Django frameworks

Cons of RQ

  • Requires Redis as a backend, limiting deployment options
  • Less flexible in terms of task scheduling compared to Huey
  • Higher memory usage for large-scale applications

Code Comparison

Huey task definition:

@huey.task()
def my_task(param):
    return f"Task completed with {param}"

RQ job definition:

from rq import Queue

q = Queue()
def my_job(param):
    return f"Job completed with {param}"

result = q.enqueue(my_job, 'example')

Both Huey and RQ are Python libraries for task queue management, but they have different approaches and features. Huey offers more flexibility in terms of storage backends and scheduling options, while RQ provides better integration with popular web frameworks and more robust job management features. The choice between the two depends on specific project requirements and infrastructure constraints.

A fast and reliable background task processing library for Python 3.

Pros of Dramatiq

  • Better support for distributed task processing across multiple machines
  • More robust error handling and retry mechanisms
  • Built-in support for middleware and result backends

Cons of Dramatiq

  • Steeper learning curve due to more complex architecture
  • Requires additional setup for distributed processing
  • Less flexible in terms of task scheduling options

Code Comparison

Huey task definition:

@huey.task()
def my_task(arg):
    return do_something(arg)

Dramatiq task definition:

@dramatiq.actor
def my_task(arg):
    return do_something(arg)

Both libraries offer similar syntax for defining tasks, but Dramatiq uses the term "actor" instead of "task". The main differences lie in the underlying architecture and execution model rather than the basic task definition syntax.

Dramatiq is generally better suited for large-scale distributed systems with complex workflows, while Huey is simpler and easier to set up for smaller projects or single-server deployments. The choice between the two depends on the specific requirements of your project, such as scalability needs, error handling complexity, and desired scheduling flexibility.

Task scheduling library for Python

Pros of APScheduler

  • More flexible scheduling options, including cron-like expressions
  • Supports multiple job stores (e.g., memory, SQLAlchemy, MongoDB)
  • Better documentation and more extensive API

Cons of APScheduler

  • More complex setup and configuration
  • Heavier resource usage for simple scheduling tasks
  • Less integrated with specific web frameworks

Code Comparison

APScheduler:

from apscheduler.schedulers.background import BackgroundScheduler

scheduler = BackgroundScheduler()
scheduler.add_job(my_job, 'interval', minutes=30)
scheduler.start()

Huey:

from huey import crontab, RedisHuey

huey = RedisHuey()

@huey.periodic_task(crontab(minute='*/30'))
def my_job():
    # Job logic here

APScheduler offers more flexibility in scheduling options and job stores, making it suitable for complex scheduling needs. However, it can be overkill for simpler tasks and requires more setup. Huey, on the other hand, provides a simpler API and integrates well with web frameworks like Django, but has fewer advanced features. The code comparison shows that APScheduler uses a more explicit configuration approach, while Huey leverages decorators for a more concise syntax.

6,795

Real-time monitor and web admin for Celery distributed task queue

Pros of Flower

  • More extensive monitoring and management capabilities through a web-based UI
  • Better support for distributed task execution across multiple workers
  • Integrates well with Celery and other task queues

Cons of Flower

  • Heavier and more complex setup compared to Huey
  • Requires additional dependencies and configuration
  • May be overkill for simpler task queue needs

Code Comparison

Huey task definition:

@huey.task()
def my_task(param):
    return do_something(param)

Flower (with Celery) task definition:

@app.task
def my_task(param):
    return do_something(param)

Summary

Huey is a lightweight and simple task queue solution, ideal for smaller projects or those with straightforward task processing needs. It's easy to set up and use, with minimal overhead.

Flower, on the other hand, is a more robust monitoring and management tool for task queues, particularly when used with Celery. It offers advanced features like real-time monitoring, task inspection, and worker control through a web interface. However, this comes at the cost of increased complexity and setup requirements.

Choose Huey for simplicity and ease of use in smaller projects, and Flower for more comprehensive task queue management in larger, distributed systems.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

.. image:: http://media.charlesleifer.com/blog/photos/huey2-logo.png

a lightweight alternative.

huey is:

  • a task queue
  • written in python
  • clean and simple API
  • redis, sqlite, file-system, or in-memory storage
  • example code <https://github.com/coleifer/huey/tree/master/examples/>_.
  • read the documentation <https://huey.readthedocs.io/>_.

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 expiration
  • task locking
  • task pipelines and chains

.. image:: http://i.imgur.com/2EpRs.jpg

At a glance

.. code-block:: python

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:

.. code-block:: pycon

>>> from demo import add_numbers
>>> res = add_numbers(1, 2)
>>> res
<Result: task 6b6f36fc-da0d-4069-b46c-c0d4ccff1df6>

>>> res()
3

Tasks can be scheduled to run in the future:

.. code-block:: pycon

>>> 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 <https://huey.readthedocs.io/en/latest/guide.html>_ or take a look at the example code <https://github.com/coleifer/huey/tree/master/examples/>_.

Running the consumer ^^^^^^^^^^^^^^^^^^^^

Run the consumer with four worker processes:

.. code-block:: console

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

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

.. code-block:: console

$ 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:

.. code-block:: console

$ 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 <https://redis.io>_ 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

See Huey documentation <https://huey.readthedocs.io/>_.

Project page

See source code and issue tracker on Github <https://github.com/coleifer/huey/>_.

Huey is named in honor of my cat:

.. image:: http://m.charlesleifer.com/t/800x-/blog/photos/p1473037658.76.jpg?key=mD9_qMaKBAuGPi95KzXYqg