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
Release files for timelooper 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| timelooper-0.1.2.tar.gz | 3.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| timelooper-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.1 kB
Release files / timelooper-0.1.2.tar.gz
| Download URL | timelooper-0.1.2.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8fa07d41e31577072634759c6be1c897c41ceb5eeb4aa7ffd27f73a2af09690e
|
|
BLAKE2b-256 checksum How to use checksums |
f29c70edcbaa776e10cc769f4810e23b6d280ce1980350ef4eb1cfa965b57144
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.8.10 Linux/5.13.0-27-generic
|
Release files / timelooper-0.1.2-py3-none-any.whl
| Download URL | timelooper-0.1.2-py3-none-any.whl |
|---|---|
| Size | 3.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a315d05ab33b9e07d7364ae7be87dbd372be7609b256a969da08a7081e343d45
|
|
BLAKE2b-256 checksum How to use checksums |
175470fa3e53d12f04c98de9eeac1dab17a84818f8efe17f79b164b46185dbe0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.8.10 Linux/5.13.0-27-generic
|