Skip to main content

Python SDK for runqy - write distributed task handlers with simple decorators

Project description

runqy logo

runqy-python

Python SDK for runqy - write distributed task handlers with simple decorators.
Documentation · Website

Installation

pip install runqy-python

Task Handlers

Create tasks that run on runqy-worker using simple decorators:

Simple Task

from runqy_python import task, run

@task
def process(payload: dict) -> dict:
    return {"message": "Hello!", "received": payload}

if __name__ == "__main__":
    run()

With Model Loading

For ML inference tasks, use @load to load models once at startup:

from runqy_python import task, load, run

@load
def setup():
    """Runs once before ready signal. Return value is passed to @task as ctx."""
    model = load_heavy_model()  # Load weights, etc.
    return {"model": model}

@task
def process(payload: dict, ctx: dict) -> dict:
    """Process tasks using the loaded model."""
    prediction = ctx["model"].predict(payload["input"])
    return {"prediction": prediction}

if __name__ == "__main__":
    run()

One-Shot Tasks

For lightweight tasks that don't need to stay loaded in memory, use run_once():

from runqy_python import task, run_once

@task
def process(payload: dict) -> dict:
    return {"result": payload["x"] * 2}

if __name__ == "__main__":
    run_once()  # Process one task and exit
Function Behavior Use case
run() Loops forever, handles many tasks ML inference (expensive load)
run_once() Handles ONE task, exits Lightweight tasks

Protocol

The SDK handles the runqy-worker stdin/stdout JSON protocol:

  1. Load phase: Calls @load function (if registered)
  2. Ready signal: Sends {"status": "ready"} after load completes
  3. Task input: Reads JSON from stdin: {"task_id": "...", "payload": {...}}
  4. Response: Writes JSON to stdout: {"task_id": "...", "result": {...}, "error": null, "retry": false}

Client (Optional)

The SDK also includes a client for enqueuing tasks to a runqy server:

from runqy_python import RunqyClient

client = RunqyClient("http://localhost:3000", api_key="your-api-key")

# Enqueue a task
task = client.enqueue("inference.default", {"input": "hello"})
print(f"Task ID: {task.task_id}")

# Check result
result = client.get_task(task.task_id)
print(f"State: {result.state}, Result: {result.result}")

Or use the convenience function:

from runqy_python import enqueue

task = enqueue(
    "inference.default",
    {"input": "hello"},
    server_url="http://localhost:3000",
    api_key="your-api-key"
)

Client API

RunqyClient(server_url, api_key, timeout=30)

  • server_url: Base URL of the runqy server
  • api_key: API key for authentication
  • timeout: Default request timeout in seconds

client.enqueue(queue, payload, timeout=300)

  • queue: Queue name (e.g., "inference.default")
  • payload: Task payload as a dict
  • timeout: Task execution timeout in seconds
  • Returns: TaskInfo with task_id, queue, state

client.get_task(task_id)

  • task_id: Task ID from enqueue
  • Returns: TaskInfo with task_id, queue, state, result, error

Exceptions

  • RunqyError: Base exception for all client errors
  • AuthenticationError: Invalid or missing API key
  • TaskNotFoundError: Task ID doesn't exist

Development

# Install in editable mode
pip install -e .

# Test task execution
echo '{"task_id":"t1","payload":{"foo":"bar"}}' | python your_model.py

# Test client import
python -c "from runqy_python import RunqyClient; print('OK')"

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

runqy_python-0.2.2.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

runqy_python-0.2.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file runqy_python-0.2.2.tar.gz.

File metadata

  • Download URL: runqy_python-0.2.2.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for runqy_python-0.2.2.tar.gz
Algorithm Hash digest
SHA256 16f6fcd1f44e133c5bf4cc9ad7915e805758a5d393e45dbd3198eaf80ec8d8f1
MD5 83666472db4f405594a822491cf6366a
BLAKE2b-256 f3f31e817c5da24d433f37eb5bdd4d3ff4065725552e998b853659f257e264c6

See more details on using hashes here.

File details

Details for the file runqy_python-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: runqy_python-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for runqy_python-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0cb12108a1648dcc4045bab8608c22b7e2d3a816de26f2f12b756d78410fa3a1
MD5 a10950bfcfeb51ca5544719b1927b245
BLAKE2b-256 af1f2df7ec0aa34256897f9ea82c31655beb939bb2e93525d848a7023bc37012

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