Skip to main content

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

Metadata

Release files for cancel-contexts 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cancel-contexts 0.1.3
File Size Uploaded
cancel_contexts-0.1.3.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cancel-contexts 0.1.3
File Interpreter ABI Platform
cancel_contexts-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / cancel_contexts-0.1.3.tar.gz

Download URL cancel_contexts-0.1.3.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ba330ebb67ba2169cba15f72d7c50638c6a250cae16dbdd9e86f0f1b5ca6863c
BLAKE2b-256 checksum
How to use checksums
b0e6ddebd7fae55019a2f675504514ec3fb67061878d58cb5e153e9cfa239d9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.7 Darwin/23.4.0

Release files / cancel_contexts-0.1.3-py3-none-any.whl

Download URL cancel_contexts-0.1.3-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12e1a33dc646651817819de91e5b45dd7c4f5eb3658c8570d0375073172c1f58
BLAKE2b-256 checksum
How to use checksums
eda2d2e45426a695584e2f320e7acd14e02c5c4c0e51a1c447c12970d10cd760
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.7 Darwin/23.4.0

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page