Skip to main content

PyBatcher

PyBatcher est une bibliothèque Python qui permet de traiter des requêtes individuelles par lots (batching).

Fonctionnalités principales

  • Traitement par lots : Regroupez automatiquement plusieurs requêtes pour les traiter en une seule opération
  • Modes de fonctionnement flexibles :
    • Par délai : traitement automatique après un temps défini
    • Par taille : traitement lorsque le lot atteint une certaine taille
    • Mode manuel : contrôle explicite du moment du traitement
  • API synchrone et asynchrone : Compatible avec le code synchrone (futures) et asynchrone (async/await)
  • Traitement parallèle : Isolation des tâches via une boucle d'événements dédiée
  • Contrôle fin : Supervision de l'état des lots et récupération des résultats

Installation

pip install pybatcher

Concepts

Batcher

Le Batcher est responsable de la gestion du regroupement des requêtes en lots. Il se charge de :

  • Créer des lots (Batch) quand nécessaire
  • Router les requêtes vers les lots appropriés
  • Gérer le cycle de vie des lots

Batch

Un Batch représente un groupe de requêtes qui sera traité ensemble. Il offre :

  • Une gestion précise de son état (prêt, démarré, en cours, terminé)
  • Des mécanismes d'annulation
  • La récupération groupée des résultats

Exemples d'utilisation

Utilisation synchrone

from pybatcher import Batcher

def process_requests(items):
    # Traitement des items par lot
    print(f"Processing {len(items)} items at once")
    return [f"Result for {item}" for item in items]

# Création d'un batcher avec une taille maximale de lot de 10
# et un délai de 2 secondes avant traitement automatique
batcher = Batcher(
    process_function=process_requests,
    batch_delay=2.0,
    max_batch_size=10
)

# Soumission de requêtes individuelles
futures = [batcher.submit_request(f"req_{i}") for i in range(25)]

# Récupération des résultats
results = [future.result() for future in futures]

# Fermeture du batcher
batcher.close()

Utilisation asynchrone

import asyncio
from pybatcher import Batcher

async def process_requests_async(items):
    # Traitement asynchrone des items par lot
    await asyncio.sleep(1)  # Simulation d'une opération asynchrone
    return [f"Async result for {item}" for item in items]

async def main():
    # Création d'un batcher avec traitement asynchrone
    batcher = Batcher(
        process_function=process_requests_async,
        min_batch_size=5,
        max_batch_size=20
    )
    
    # Traitement asynchrone des requêtes
    tasks = []
    for i in range(50):
        task = asyncio.create_task(batcher.handle_request(f"async_req_{i}"))
        tasks.append(task)
    
    # Attente des résultats
    results = await asyncio.gather(*tasks)
    
    # Fermeture du batcher
    batcher.close()
    
    return results

# Exécution de la fonction principale
results = asyncio.run(main())

Paramètres de configuration

Batcher

  • process_function : Fonction qui traite les données en lot (peut être synchrone ou asynchrone)
  • batch_delay : Délai en secondes avant traitement automatique du lot
  • min_batch_size : Taille minimale du lot pour démarrer le traitement
  • max_batch_size : Taille maximale du lot, déclenchant un traitement immédiat
  • strict_batch_size : Si True, attend que le lot atteigne exactement la taille minimale
  • loop : Boucle d'événements asyncio à utiliser (en crée une nouvelle si non spécifiée)

Batch

  • max_size : Taille maximale du lot
  • min_size : Taille minimale du lot pour le traitement
  • strict_size : Compter uniquement les requêtes non annulées pour la taille
  • auto_start_delay : Délai avant démarrage automatique du traitement
  • auto_start_when_full : Démarrer automatiquement quand le lot est plein

Tests

La bibliothèque est fournie avec une suite de tests unitaires disponibles dans le répertoire tests/.

python -m unittest discover tests

Licence

PyBatcher est distribué sous licence libre.

Metadata

Release files for pybatcher 1.0.1

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

Source distribution (sdist)

Source distribution for pybatcher 1.0.1
File Size Uploaded Provenance Actions
pybatcher-1.0.1.tar.gz 18.5 kB Without provenance Details for pybatcher-1.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 31.3 kB

Release files / pybatcher-1.0.1.tar.gz

Download URL pybatcher-1.0.1.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
df00b176279324e77f031e8717208e8556dce8746c680959b15e03ee98e5415c
BLAKE2b-256 checksum
How to use checksums
cd222c43f4b2239014dbef084cb717867ab997117a0f1ef25d2ad82e2e60177a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / pybatcher-1.0.1-py3-none-any.whl

Download URL pybatcher-1.0.1-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a11abf86e2c0a9da795bebb7772915fc4db703b3b5555ee7acb17a4bec20cef
BLAKE2b-256 checksum
How to use checksums
7121e8029ac0522ff88aee4abbecf76e5776a089c731861a383ca991a7f321f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for pybatcher 1.0.1

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

1.0.1 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