Sometimes, within your systems, you may wish to cancel jobs before they are processed by your executor objects. This would be analogous to cancelling, say, a print job in real life--we've somehow decided that we no longer need to print out a document within our print queue, and we cancel it.
However, on a typical printer, we can't cancel the task when it's midway through execution. This also applies to tasks that we submit to either our ThreadPoolExecutors or ProcessPoolExecutors.
Cancelling tasks submitted to an executor can be done by calling the cancel() function on that specific task as follows:
with ThreadPoolExecutor(max_workers=2) as executor:
myTask = executor.submit(someTask, (1))
print(myTask.cancel())
The cancel function returns a boolean value which is either true if we successfully managed to cancel the future object, or false if unsuccessful. In the preceding example, you would see that it returns false unless you submit jobs prior to myTask that keeps the executor object occupied.