Skip to main content

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)

Uploaded Source

Built Distribution

cancel_contexts-0.1.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

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

Hashes for cancel_contexts-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ba330ebb67ba2169cba15f72d7c50638c6a250cae16dbdd9e86f0f1b5ca6863c
MD5 4ccc35f0cbba20be859cdfec048626af
BLAKE2b-256 b0e6ddebd7fae55019a2f675504514ec3fb67061878d58cb5e153e9cfa239d9b

See more details on using hashes here.

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

Hashes for cancel_contexts-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 12e1a33dc646651817819de91e5b45dd7c4f5eb3658c8570d0375073172c1f58
MD5 31d36b23ffa4e5a26c54866bf8616c43
BLAKE2b-256 eda2d2e45426a695584e2f320e7acd14e02c5c4c0e51a1c447c12970d10cd760

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page