Cancel and read the status

Each token object has a cancelled attribute and a cancel() method. Using the attribute, you can find out whether this token has been cancelled:

from cantok import SimpleToken

token = SimpleToken()
print(token.cancelled)  #> False
token.cancel()
print(token.cancelled)  #> True

The cancelled attribute is dynamically calculated and takes into account, among other things, conditions specific to that token type. Here is an example with a token that measures time:

from time import sleep
from cantok import TimeoutToken

token = TimeoutToken(5)
print(token.cancelled)  #> False
sleep(10)
print(token.cancelled)  #> True

In addition to this attribute, each token implements the is_cancelled() method. It does exactly the same thing as the attribute:

from cantok import SimpleToken

token = SimpleToken()
print(token.cancelled)  #> False
print(token.is_cancelled())  #> False
token.cancel()
print(token.cancelled)  #> True
print(token.is_cancelled())  #> True

Choose what you like best. The attribute is considered more elegant, but calling the method more clearly reflects the complexity of the work actually being done to answer the question "has the token been cancelled?".

There is another method opposite to is_cancelled()keep_on(). It answers the opposite question, and can be used in the same situations:

from cantok import SimpleToken

token = SimpleToken()
print(token.cancelled)  #> False
print(token.keep_on())  #> True
token.cancel()
print(token.cancelled)  #> True
print(token.keep_on())  #> False

You don't have to call the keep_on() method directly. Use the token itself as a boolean value, and the method call will occur "under the hood" automatically:

from cantok import SimpleToken

token = SimpleToken()
print(bool(token))  #> True
print(token.keep_on())  #> True
token.cancel()
print(bool(token))  #> False
print(token.keep_on())  #> False

There is another method that is close in meaning to is_cancelled()check(). It does nothing if the token is not cancelled, or raises an exception if it is cancelled. If the token was cancelled by calling the cancel() method, a CancellationError exception will be raised:

from cantok import SimpleToken

token = SimpleToken()
token.check()  # Nothing happens.
token.cancel()
token.check()
#> ...
#> cantok.errors.CancellationError: The token has been cancelled.

Otherwise, a special exception inherited from CancellationError will be raised:

from cantok import TimeoutToken

token = TimeoutToken(0)
token.check()
#> ...
#> cantok.errors.TimeoutCancellationError: The timeout of 0 seconds has expired.