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 details)
Built Distribution
File details
Details for the file cancel_contexts-0.1.3.tar.gz
.
File metadata
- Download URL: cancel_contexts-0.1.3.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.7 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba330ebb67ba2169cba15f72d7c50638c6a250cae16dbdd9e86f0f1b5ca6863c |
|
MD5 | 4ccc35f0cbba20be859cdfec048626af |
|
BLAKE2b-256 | b0e6ddebd7fae55019a2f675504514ec3fb67061878d58cb5e153e9cfa239d9b |
File details
Details for the file cancel_contexts-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: cancel_contexts-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.7 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12e1a33dc646651817819de91e5b45dd7c4f5eb3658c8570d0375073172c1f58 |
|
MD5 | 31d36b23ffa4e5a26c54866bf8616c43 |
|
BLAKE2b-256 | eda2d2e45426a695584e2f320e7acd14e02c5c4c0e51a1c447c12970d10cd760 |