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.2.tar.gz
(10.8 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.2.tar.gz.
File metadata
- Download URL: mic_worker-0.1.2.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c9f26b941e3620cfa0125c73f45049ef79935b1d9d5c6ea682fc9183bdefb55
|
|
| MD5 |
fcea8b87464266ccd8f8f3f76dfdb23c
|
|
| BLAKE2b-256 |
10835603636d3e287f659872af26f436d7a8bed9043ae15bbb98eb5c58f537fc
|
File details
Details for the file mic_worker-0.1.2-py3-none-any.whl.
File metadata
- Download URL: mic_worker-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
036f0a8ae15b0acca5f6f936dacedcc8ef8d7cfd3d3f562f8c79d71d5762ef0e
|
|
| MD5 |
a8f13bc2f8b3e0dc14ced6590201ce03
|
|
| BLAKE2b-256 |
f251c025c676f84cb8a4a48d3a9fa23e0f983e6f811caf62fb4cff4e4f0ccb6d
|