Timed loops made simple
Project description
Timelooper
I found myself re-implementing the same pattern over and over when it comes to repeating some task until some condition is met OR the time is up, so here it is abstracted and generalized into a neat package.
Yep, that's 25 lines of code + tests.
Here's a demo use case:
from timelooper import Looped, loop_timed
from datetime import timedelta
# Suppose we are listening to some queue
# and want to batch the incoming messages.
# However, we only want to wait for
# some limited time for a batch
# to be formed.
class CollectableBatch(Looped):
def __init__(self, queue, maxsize):
self.batch = []
self._queue = queue
self._maxsize = maxsize
async def do(self) -> None:
self.batch.append(await self._queue.get())
def should_stop(self) -> bool:
return len(self.batch) == self._maxsize
collected = CollectableBatch(queue, maxsize=10)
await loop_timed(collected, timedelta(seconds=30))
print(collected.batch) # or whatever
Installation
pip install timelooper
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
timelooper-0.1.1.tar.gz
(2.9 kB
view hashes)
Built Distribution
Close
Hashes for timelooper-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af5581dd94ff4bd734b8c6366c5e0d1884ba725ec21614f404d9bce42e7d29b5 |
|
MD5 | 8fd38731a032b9700d7cadea349fd147 |
|
BLAKE2b-256 | c0492bb3c8425c6292abb9aad512e33784f36d60aba89ee6258a33adf9f8253e |