https://github.com/monomonedula/timelooper
Timed loops made simple
https://github.com/monomonedula/timelooper
asyncio loop looping python timer utility
Last synced: 12 months ago
JSON representation
Timed loops made simple
- Host: GitHub
- URL: https://github.com/monomonedula/timelooper
- Owner: monomonedula
- License: mit
- Created: 2022-01-22T21:21:32.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-02-01T22:08:35.000Z (over 4 years ago)
- Last Synced: 2025-05-15T05:51:16.596Z (about 1 year ago)
- Topics: asyncio, loop, looping, python, timer, utility
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Timelooper
[](https://app.travis-ci.com/monomonedula/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:
```python
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
```shell
pip install timelooper
```