Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pomponchik/cantok
Implementation of the "Cancellation Token" pattern
https://github.com/pomponchik/cantok
cancellationtoken threading threading-synchronization
Last synced: about 2 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 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-27T11:11:40.000Z (4 months ago)
- Last Synced: 2024-08-28T11:45:55.856Z (4 months ago)
- Topics: cancellationtoken, threading, threading-synchronization
- Language: Python
- Homepage: http://cantok.readthedocs.io/
- Size: 1.19 MB
- Stars: 99
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![logo](https://raw.githubusercontent.com/pomponchik/cantok/main/docs/assets/logo_5.png)
[![Downloads](https://static.pepy.tech/badge/cantok/month)](https://pepy.tech/project/cantok)
[![Downloads](https://static.pepy.tech/badge/cantok)](https://pepy.tech/project/cantok)
[![codecov](https://codecov.io/gh/pomponchik/cantok/graph/badge.svg?token=eZ4eK6fkmx)](https://codecov.io/gh/pomponchik/cantok)
[![Lines of code](https://sloc.xyz/github/pomponchik/cantok/?category=code)](https://github.com/boyter/scc/)
[![Hits-of-Code](https://hitsofcode.com/github/pomponchik/cantok?branch=main)](https://hitsofcode.com/github/pomponchik/cantok/view?branch=main)
[![Test-Package](https://github.com/pomponchik/cantok/actions/workflows/tests_and_coverage.yml/badge.svg)](https://github.com/pomponchik/cantok/actions/workflows/tests_and_coverage.yml)
[![Python versions](https://img.shields.io/pypi/pyversions/cantok.svg)](https://pypi.python.org/pypi/cantok)
[![PyPI version](https://badge.fury.io/py/cantok.svg)](https://badge.fury.io/py/cantok)
[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](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, TimeoutTokentoken = ConditionToken(lambda: randint(1, 100_000) == 1984) + CounterToken(400_000, direct=False) + TimeoutToken(1)
counter = 0while token:
counter += 1print(counter)
```Read more in the [documentation](https://cantok.readthedocs.io/en/latest/)!