Skip to main content

Intelligent API request distribution based on system resources and latency

Project description

distribAPI

distribAPI is a lightweight Python library for intelligent API request distribution.
It dynamically routes requests to the best available endpoint or to a local handler, based on:

  • CPU, RAM, and optionally GPU usage
  • Endpoint latency
  • Failure rate
  • Endpoint priority

It is designed for distributed applications where load balancing and resource-aware routing are important.

Features

  • Asynchronous request dispatching using aiohttp
  • Automatic endpoint selection based on resource usage and latency
  • Local fallback processing when endpoints are overloaded or unavailable
  • Optional GPU monitoring
  • Retry logic for failed requests
  • Detailed endpoint statistics

Installation

pip install distribapi

Optional GPU monitoring:

pip install distribapi[gpu]

For development:

pip install distribapi[dev]

Quick example

import asyncio
from distribapi import distrib

async def local_handler(data):
    return {"processed_locally": True, "echo": data}

# Initialize distribAPI
distrib.init(resources=["cpu", "ram"], latency=True)

# Add API endpoints
distrib.add_endpoint("http://localhost:8001/api", "Node-1")
distrib.add_endpoint("http://localhost:8002/api", "Node-2")

# Set local handler
distrib.set_local_handler(local_handler)

async def main():
    result = await distrib.process({"input": "Hello World"})
    print("Result:", result)
    print("Stats:", distrib.get_stats())

asyncio.run(main())

Usage

Initialise

distrib.init(
    resources=["cpu", "ram", "gpu"],  # GPU is optional and needs the GPU-installation
    latency=True,
    fallback_local=True
)

Add Endpoints

distrib.add_endpoint(
    url="https://server1.example.com/infer",
    name="Server-1",
    priority=1,
    max_cpu=80.0,
    max_ram=80.0,
    max_gpu=80.0,
    timeout=30.0
)

Local Handler

async def local_handler(data):
    return {"status": "processed locally"}

distrib.set_local_handler(local_handler)

Process a Request

response = await distrib.process({"text": "example"})

Response example:

{
  "success": true,
  "data": {...},
  "endpoint": "Server-1",
  "latency": 0.042
}

Get Statistics

stats = distrib.get_stats()
print(stats)

Example:

{
  "endpoints": [
    {
      "name": "Server-1",
      "url": "...",
      "avg_latency": 0.221,
      "request_count": 42,
      "failed_requests": 3,
      "success_rate": 92.85
    }
  ],
  "system_resources": {
    "cpu": 17.4,
    "ram": 48.2
  }
}

License

This project is licensed under the MIT License.

Contributing

Pull requests and issues are welcome. For major changes, please open a discussion first.

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

distribapi-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

distribapi-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file distribapi-0.1.0.tar.gz.

File metadata

  • Download URL: distribapi-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for distribapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37090d679df4bc9546851e4cdd571377582b9dc4476d43b0d9a934ea09f612f3
MD5 1a534b0662215e66e74fd9277d505cf5
BLAKE2b-256 5b005cdbea8dd80fcce4332d2142aac8d000ef007f22f11f12bcb2142a547e8b

See more details on using hashes here.

File details

Details for the file distribapi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: distribapi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for distribapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9706a2007bc57417be3e820513d37dd07854e4aa8f2bd838cef20c76e2857e16
MD5 39966309d950daf6cdf62a41380645ab
BLAKE2b-256 b88f8f845e61bf8da4577151a236a7f9657c08a00249a4abfcc845359166ff44

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