Python SDK for runqy - write distributed task handlers with simple decorators
Project description
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:
- Load phase: Calls
@loadfunction (if registered) - Ready signal: Sends
{"status": "ready"}after load completes - Task input: Reads JSON from stdin:
{"task_id": "...", "payload": {...}} - 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 serverapi_key: API key for authenticationtimeout: Default request timeout in seconds
client.enqueue(queue, payload, timeout=300)
queue: Queue name (e.g.,"inference.default")payload: Task payload as a dicttimeout: Task execution timeout in seconds- Returns:
TaskInfowithtask_id,queue,state
client.get_task(task_id)
task_id: Task ID from enqueue- Returns:
TaskInfowithtask_id,queue,state,result,error
Exceptions
RunqyError: Base exception for all client errorsAuthenticationError: Invalid or missing API keyTaskNotFoundError: 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16f6fcd1f44e133c5bf4cc9ad7915e805758a5d393e45dbd3198eaf80ec8d8f1
|
|
| MD5 |
83666472db4f405594a822491cf6366a
|
|
| BLAKE2b-256 |
f3f31e817c5da24d433f37eb5bdd4d3ff4065725552e998b853659f257e264c6
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cb12108a1648dcc4045bab8608c22b7e2d3a816de26f2f12b756d78410fa3a1
|
|
| MD5 |
a10950bfcfeb51ca5544719b1927b245
|
|
| BLAKE2b-256 |
af1f2df7ec0aa34256897f9ea82c31655beb939bb2e93525d848a7023bc37012
|