Skip to main content

Celery-like task queue for ML inference.

Project description

ModelQ

ModelQ Logo

PyPI version

ModelQ is a lightweight Python library for scheduling and queuing machine learning inference tasks. It's designed as a faster and simpler alternative to Celery for ML workloads, using Redis and threading to efficiently run background tasks.

ModelQ is developed and maintained by the team at Modelslab.

About Modelslab: Modelslab provides powerful APIs for AI-native applications including:

  • Image generation
  • Uncensored chat
  • Video generation
  • Audio generation
  • And much more

🚀 Features

  • ✅ Retry support (automatic and manual)
  • ⏱ Timeout handling for long-running tasks
  • 🔁 Manual retry using RetryTaskException
  • 🛄 Streaming results from tasks in real-time
  • 🧹 Middleware hooks for task lifecycle events
  • ⚡ Fast, non-blocking concurrency using threads
  • 🧵 Built-in decorators to register tasks quickly
  • 🗃 Redis-based task queueing

📦 Installation

pip install modelq

🧠 Basic Usage

from modelq import ModelQ
from modelq.exceptions import RetryTaskException
from redis import Redis
import time

imagine_db = Redis(host="localhost", port=6379, db=0)
q = ModelQ(redis_client=imagine_db)

@q.task(timeout=10, retries=2)
def add(a, b):
    return a + b

@q.task(stream=True)
def stream_multiples(x):
    for i in range(5):
        time.sleep(1)
        yield f"{i+1} * {x} = {(i+1) * x}"

@q.task()
def fragile(x):
    if x < 5:
        raise RetryTaskException("Try again.")
    return x

q.start_workers()

task = add(2, 3)
print(task.get_result(q.redis_client))

⚙️ Middleware Support

ModelQ allows you to plug in custom middleware to hook into events:

Supported Events

  • before_worker_boot
  • after_worker_boot
  • before_worker_shutdown
  • after_worker_shutdown
  • before_enqueue
  • after_enqueue
  • on_error

Example

from modelq.app.middleware import Middleware

class LoggingMiddleware(Middleware):
    def before_enqueue(self, *args, **kwargs):
        print("Task about to be enqueued")

    def on_error(self, task, error):
        print(f"Error in task {task.task_id}: {error}")

Attach to ModelQ instance:

q.middleware = LoggingMiddleware()

🛠 Configuration

Connect to Redis using custom config:

from redis import Redis

imagine_db = Redis(host="localhost", port=6379, db=0)
modelq = ModelQ(
    redis_client=imagine_db,
    delay_seconds=10,  # delay between retries
    webhook_url="https://your.error.receiver/discord-or-slack"
)

📜 License

ModelQ is released under the MIT License.


🤝 Contributing

We welcome contributions! Open an issue or submit a PR at github.com/modelslab/modelq.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modelq-0.1.28.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modelq-0.1.28-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file modelq-0.1.28.tar.gz.

File metadata

  • Download URL: modelq-0.1.28.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.1 Linux/6.8.0-1021-azure

File hashes

Hashes for modelq-0.1.28.tar.gz
Algorithm Hash digest
SHA256 5d5283c6fdf293cd446a237259d205dc11ebfda32b7fc698dcfab54c38eff6f7
MD5 7b81e1c09288821cc49e23eb11fd8a83
BLAKE2b-256 5776dbec578115c9e81c09642093dd976d36a8a41fa3586309ca9d2e7bf02b97

See more details on using hashes here.

File details

Details for the file modelq-0.1.28-py3-none-any.whl.

File metadata

  • Download URL: modelq-0.1.28-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.1 Linux/6.8.0-1021-azure

File hashes

Hashes for modelq-0.1.28-py3-none-any.whl
Algorithm Hash digest
SHA256 bf9bc8db70af9f354b9e01acd56b1e075577d9215487cbbeb7be98906f25c13a
MD5 2baf2f2d0babfa3b4933f8721514bf8f
BLAKE2b-256 b9df5d52f90acf344fa24b1829c1d5c014fcc88001d3d2b668f46de9ec4ba3f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page