https://github.com/pomponchik/cantok
Implementation of the "Cancellation Token" pattern
https://github.com/pomponchik/cantok
cancellationtoken threading threading-synchronization
Last synced: 11 months ago
JSON representation
Implementation of the "Cancellation Token" pattern
- Host: GitHub
- URL: https://github.com/pomponchik/cantok
- Owner: pomponchik
- License: mit
- Created: 2023-09-18T19:46:50.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-01T20:16:22.000Z (12 months ago)
- Last Synced: 2025-03-01T20:23:12.944Z (12 months ago)
- Topics: cancellationtoken, threading, threading-synchronization
- Language: Python
- Homepage: http://cantok.readthedocs.io/
- Size: 1.14 MB
- Stars: 117
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://pepy.tech/project/cantok)
[](https://pepy.tech/project/cantok)
[](https://codecov.io/gh/pomponchik/cantok)
[](https://github.com/boyter/scc/)
[](https://hitsofcode.com/github/pomponchik/cantok/view?branch=main)
[](https://github.com/pomponchik/cantok/actions/workflows/tests_and_coverage.yml)
[](https://pypi.python.org/pypi/cantok)
[](https://badge.fury.io/py/cantok)
[](http://mypy-lang.org/)
[](https://github.com/astral-sh/ruff)
Cancellation Token is a pattern that allows us to refuse to continue calculations that we no longer need. It is implemented out of the box in many programming languages, for example in [C#](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken) and in [Go](https://pkg.go.dev/context). However, there was still no sane implementation in Python, until the [cantok](https://github.com/pomponchik/cantok) library appeared.
## Quick start
Install [it](https://pypi.org/project/cantok/):
```bash
pip install cantok
```
And use:
```python
from random import randint
from cantok import ConditionToken, CounterToken, TimeoutToken
token = ConditionToken(lambda: randint(1, 100_000) == 1984) + CounterToken(400_000, direct=False) + TimeoutToken(1)
counter = 0
while token:
counter += 1
print(counter)
```
Read more in the [documentation](https://cantok.readthedocs.io/en/latest/)!