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.2.tar.gz
(3.0 kB
view hashes)
Built Distribution
Close
Hashes for timelooper-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a315d05ab33b9e07d7364ae7be87dbd372be7609b256a969da08a7081e343d45 |
|
MD5 | a507116aeba2139025509b3317b8eb64 |
|
BLAKE2b-256 | 175470fa3e53d12f04c98de9eeac1dab17a84818f8efe17f79b164b46185dbe0 |