Skip to main content

LightInfer

LightInfer is a lightweight, high-performance bridge for serving synchronous model inference code (PyTorch, TensorFlow, etc.) via an asynchronous FastAPI server.

It solves the "Blocking Loop" problem by efficiently isolating heavy computation in dedicated worker threads while maintaining a fully asynchronous, high-concurrency web frontend.

Features

  • Zero-Blocking Architecture: Async Web Frontend + Sync Worker Threads.
  • Efficient Bridge: Uses AsyncResponseBridge for zero-thread-overhead waiting.
  • Streaming Support:
    • Native Server-Sent Events (SSE) for text streaming.
    • Binary Streaming for audio/video generation (with chunk buffering).
  • Easy Integration: Wrap any Python class with an infer method.
  • Context Isolation: Each worker runs in its own thread, ensuring safety for libraries like PyTorch.

Installation

pip install lightinfer

Quick Start

1. Define your Model

LightInfer wraps any class with an infer method. The arguments to infer are automatically mapped from the JSON request.

import time

class MyModel:
    def infer(self, prompt: str = "world"):
        # Simulate heavy work
        time.sleep(1)
        return {"message": f"Hello, {prompt}!"}

2. Start the Server

from lightinfer.server import LightServer

# Create your model instance
model = MyModel()

# Start server (you can pass a list of models to run multiple worker threads)
server = LightServer([model])
server.start(port=8000)

3. Make Requests

Standard Request:

import requests

# 'args' in JSON maps to positional arguments of infer()
# 'kwargs' in JSON maps to keyword arguments of infer()
resp = requests.post("http://localhost:8000/api/v1/infer", 
                     json={"args": ["LightInfer"]})
print(resp.json())
# Output: {'message': 'Hello, LightInfer!'}

Streaming Request:

If your model returns a generator, you can use streaming:

class StreamingModel:
    def infer(self, prompt: str):
        yield "Part 1"
        time.sleep(0.5)
        yield "Part 2"

Client side:

resp = requests.post("http://localhost:8000/api/v1/infer", 
                     json={"args": ["test"], "stream": True}, stream=True)

for line in resp.iter_lines():
    if line:
        print(line.decode('utf-8'))

Examples

Check the examples/ directory for ready-to-run scenarios:

  • Simple LLM: Text-to-Text generation with SSE streaming.
  • Streaming TTS: Text-to-Audio generation with binary chunk streaming.

CLI Usage

You can serve any model class directly from the terminal.

Format: lightinfer <module>:<Class>

Given a file my_model.py:

class MyModel:
    def infer(self, prompt: str):
        return f"Echo: {prompt}"

Run:

lightinfer my_model:MyModel --port 8000 --workers 2

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Metadata

Release files for lightinfer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lightinfer 0.1.0
File Size Uploaded Provenance Actions
lightinfer-0.1.0.tar.gz 8.7 kB Without provenance Details for lightinfer-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lightinfer 0.1.0
File Interpreter ABI Platform Actions
lightinfer-0.1.0-py3-none-any.whl Python 3 none any Details for lightinfer-0.1.0-py3-none-any.whl

Total release size: 16.2 kB

Release files / lightinfer-0.1.0.tar.gz

Download URL lightinfer-0.1.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fe1916b9f94c701729004b5477f9e26f2eaeb69b6f5fd99d885d1e70abd1aa32
BLAKE2b-256 checksum
How to use checksums
9d19389f0cd82486881adf641482173df38a66f12c1227a2722f8366ba68afa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / lightinfer-0.1.0-py3-none-any.whl

Download URL lightinfer-0.1.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2daa94f9b9b465b2885ad48bf33b3e39525e5ff7d0a37ce90a69c45aec64e34
BLAKE2b-256 checksum
How to use checksums
147389874283dbd10038fa2d19b9c33827541bd68b628f1ac7e7e967e8d41ac9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Security for lightinfer 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page