Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cgarciae/stop_iter
https://github.com/cgarciae/stop_iter
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/cgarciae/stop_iter
- Owner: cgarciae
- License: apache-2.0
- Created: 2024-07-10T14:56:11.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-08-02T09:26:04.000Z (6 months ago)
- Last Synced: 2024-08-02T10:56:19.418Z (6 months ago)
- Language: Python
- Size: 11.7 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stop_iter
``stop_iter`` takes an iterable and returns a new iterable that can be safely stopped using ``SIGINT``. This is useful when you have a long-running computation, such as machine learning training loops, that you want to be able to stop without losing all of the work that has been done so far.
## Installation
```bash
pip install stop_iter
```## Usage
The following code will print the integers until you press ``Ctrl+C`` and then print "infinity".```python
from stop_iter import stop_iterdef integers():
count = 0
while True:
yield (count := count + 1)for n in stop_iter(integers()):
print(n)print("\ninfinity")
```
Also works as a context manager:
```python
with stop_iter() as it:
for n in integers():
print(n)
if it.interrupt:
break
```
And as a decorator:
```python
@(it := stop_iter())
def print_integers():
for n in integers():
print(n)
if it.interrupt:
breakprint_integers()
```