pomponchik / cantok

Implementation of the "Cancellation Token" pattern

Home Page:http://cantok.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo

Downloads Downloads codecov Lines of code Hits-of-Code Test-Package Python versions PyPI version Checked with mypy 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# and in Go. However, there was still no sane implementation in Python, until the cantok library appeared.

Quick start

Install it:

pip install cantok

And use:

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!

About

Implementation of the "Cancellation Token" pattern

http://cantok.readthedocs.io/

License:MIT License


Languages

Language:Python 100.0%