Worker package for async tasks
Project description
Async Worker Package
Un package Python pour créer des workers asynchrones qui traitent des tâches à partir de queues RabbitMQ.
Fonctionnalités
- Support des tâches synchrones et asynchrones
- Gestion automatique des connexions RabbitMQ avec reconnexion
- Health check HTTP intégré
- Gestion des signaux de shutdown gracieux
- Support du mode one-shot et infinite avec concurrence configurable
- Logging structuré avec callbacks de progression
Installation
Via pip
pip install mic-worker
Via uv
uv add mic-worker
Utilisation
Exemple de tâche asynchrone
from async_worker import AsyncTaskInterface, IncomingMessage
import asyncio
class MyAsyncTask(AsyncTaskInterface):
async def execute(self, incoming_message: IncomingMessage, progress):
# Votre logique de traitement ici
await asyncio.sleep(1)
await progress(0.5) # Reporter le progrès
await asyncio.sleep(1)
return {"result": "success"}
Exemple de tâche synchrone
from async_worker import SyncTaskInterface, IncomingMessage
class MySyncTask(SyncTaskInterface):
def execute(self, incoming_message: IncomingMessage, progress):
# Votre logique de traitement ici
time.sleep(1)
progress(0.5) # Reporter le progrès
time.sleep(1)
return {"result": "success"}
Configuration du runner
from async_worker import AsyncWorkerRunner, Infinite, HealthCheckConfig
runner = AsyncWorkerRunner(
amqp_url="amqp://localhost:5672",
amqp_in_queue="input_queue",
amqp_out_queue="output_queue",
task_provider=lambda: MyAsyncTask(),
worker_mode=Infinite(concurrency=5),
health_check_config=HealthCheckConfig(host="0.0.0.0", port=8000)
)
await runner.start()
Intégration conteneurisé
Construction de l'image
docker build -t python-worker .
Lancement du conteneur
docker run -e BROKER_URL="amqp://rabbitmq:5672" \
-e IN_QUEUE_NAME="my_input_queue" \
-e OUT_QUEUE_NAME="my_output_queue" \
-e WORKER_CONCURRENCY="3" \
-p 8000:8000 \
python-worker
Variables d'environnement
BROKER_URL: URL de connexion RabbitMQ (obligatoire)IN_QUEUE_NAME: Nom de la queue d'entrée (défaut: "in_queue_python")OUT_QUEUE_NAME: Nom de la queue de sortie (défaut: "example_out_queue")WORKER_CONCURRENCY: Nombre de tâches concurrentes (défaut: "5")
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
mic_worker-0.1.1.tar.gz
(8.2 kB
view details)
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 mic_worker-0.1.1.tar.gz.
File metadata
- Download URL: mic_worker-0.1.1.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9718c96efd0f921c6955fd9c3982053f406fd9ef809676e1cfb33f5dc2dd8254
|
|
| MD5 |
072b68ddf7bc06f1a60a75429a542442
|
|
| BLAKE2b-256 |
75c19644d0212543d72c11dc879ceb13ae974e344dec4332657c2353570025bb
|
File details
Details for the file mic_worker-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mic_worker-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
568cfb99ea11b21e1718bb03f0963732e34646cc1bfadacf468c282f0ff9653b
|
|
| MD5 |
16f879355c00a25fcc6abe4c58e90a63
|
|
| BLAKE2b-256 |
77b3382c73cd7d7c5ac4585173a98ed815badae960b5a7c81b474f019e31ae4c
|