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-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-1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: distribapi-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-1.0.tar.gz
Algorithm Hash digest
SHA256 928ad71050757d6af14e742f9cc55f24c28722299279fc5b5cbac0cc353d4c14
MD5 5d33c0de6e61041fa32ac397585fa23f
BLAKE2b-256 7cb7a374119e7f7e43bc94bc285d5e011043ccfb6b3225c420366b5f29000c81

See more details on using hashes here.

File details

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

File metadata

  • Download URL: distribapi-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-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8c85c5f0b1f66a4e18fd84e4bd4a0f6b94d5851fecb50d227aa232bd3f80173
MD5 9214dbe757df1ea215fe64e65000585e
BLAKE2b-256 df271c18b9691cba4f5981de16eb5e57e43d87129fe78eaf5679317e399a91d1

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