Hello! đź‘‹
The Cancellation Token Pattern article is a pattern inspired by C#’s CancellationToken struct and Golang’s context package.
The main idea of the pattern is to allow the user of an API to cancel its operations, but in order for this pattern to work, the API must be written with the cancellation token pattern in mind.
To use the pattern, you need a class that represents a cancellation token. Here’s my simple version:
|
|
Since cancel()
is an idempotent operation the CancellationToken
class can be safely used in a multithreaded
application.
The code of the API must be written with the CancellationToken
in mind.
For example, consider the following piece of code is not written with the pattern in mind:
|
|
It loops forever, sleeps for a second and prints the string “Hello World.” We can rewrite it using the cancellation token pattern like this:
|
|
The code now checks if the token is canceled, and if it is, it will stop running.
The user of the API has more control to when to cancel the execution of the operation/code.
Consider this example which cancels the token when it receives the SIGINT signal.
The signal SIGINT is sent by the operating system to the program when we press CTRL+C
inside the terminal
or click the stop button in the IntelliJ IDE:
|
|
If an API has a more complex control flow like running multiple functions in parallel, we can pass the initial cancellation token to each function and cancel all of them at once.
Golang has WithTimeout which creates a context that gets cancelled after
the specified timeout. To implement a similar feature in our CancellationToken we can subclass it in our
Python implementation and use Timer object to cancel
the token. Alternatively, we can add the timeout to the current time and call it timeout_time
,
and when is_canceled()
is called we can check if the current time is greater than or equal (>=
)
than the timeout_time
.
I hope you’ve enjoyed this article and thank you for reading! 🙏
If you have any thoughts about this article, feel free to leave a comment.