Python library for efficiently batch-processing workloads asynchronously with batch-size- and time-based flushes.
Project description
Timed Batch Worker
Python library for efficiently batch-processing workloads asynchronously with batch-size- and time-based flushes using short-circuiting events.
Installation
pip install timed-batch-worker
Usage
from timed_batch_worker import TimedBatchWorker
# Handle the batched items
def handle_batch(items):
http.post('/api/logs/bulk', data=items)
# Create a worker that flushes the queue when:
# 1. The batch contains 20 objects, or
# 2. Five seconds have passed since the last flush
worker = TimedBatchWorker(
handler=handle_batch,
flush_interval=5,
flush_batch_size=20,
)
# Worker started in separate thread to avoid throttling main event loop
# in case the batch handler includes synchronous workloads.
worker.start()
# Add objects to the current batch at any time
@app.get('/users/create')
def create_user():
...
# Hand off an object to the worker
worker.enqueue({
'event': 'USER_CREATED',
'data': user
})
...
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
Built Distribution
Close
Hashes for timed_batch_worker-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ffb3066f7d8dc34926e65ce64bd730472f2d0af8a961fccbb523983c3c01bc5 |
|
MD5 | dfa2b9ab32c590d31180e0ba1c3d4c7d |
|
BLAKE2b-256 | 3d02f3046e754e1fc144975bcfd1ce9c4f34603997111a93fb84987d1ae9e5eb |