Realisation for canceling contexts (cancelled tokens) for Python
Project description
Cancel-contexts
This is a simple implementation of cancel-contexts (cancel tokens is C#) in Python. It is inspired by the Go programming language's context package.
Installation
poetry add cancel-contexts
or
pip install cancel-contexts
Usage
from cancel_contexts import CancelContext
ctx = CancelContext()
print(ctx.cancelled) # False
print(bool(ctx)) # True
ctx.cancel()
print(ctx.cancelled) # True
print(bool(ctx)) # False
from cancel_contexts import CancelContext
from cancel_contexts.exceptions import ContextCancelledError
ctx = CancelContext()
counter = 0
while ctx:
counter += 1
if counter == 10:
ctx.cancel()
print(ctx.cancelled) # True
try:
ctx.check_cancelled()
except ContextCancelledError as e:
print(e) # Context was cancelled
from time import sleep
from cancel_contexts import TimeOutContext
from cancel_contexts.exceptions import ContextTimeOutError
ctx = TimeOutContext(10)
print(ctx.cancelled) # False
while ctx:
sleep(1)
print(ctx.cancelled) # False
print(ctx.cancelled) # True
try:
ctx.check_cancelled()
except ContextTimeOutError as e:
print(e) # Context was timed out
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cancel_contexts-0.1.3.tar.gz
(3.5 kB
view hashes)
Built Distribution
Close
Hashes for cancel_contexts-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12e1a33dc646651817819de91e5b45dd7c4f5eb3658c8570d0375073172c1f58 |
|
MD5 | 31d36b23ffa4e5a26c54866bf8616c43 |
|
BLAKE2b-256 | eda2d2e45426a695584e2f320e7acd14e02c5c4c0e51a1c447c12970d10cd760 |