Let's take a look at this in more detail in a full-fledged example. Here, we define some arbitrary task to keep our ThreadPoolExecutor occupied--this is called someTask, and prints out that it's executing something before sleeping for n seconds.
In our main function, we again use the with command to instantiate our ThreadPoolExecutor as a context manager, and within the boundaries of this context manager, we then submit four distinct tasks. Immediately after submitting all four tasks, we then attempt to print the outcome of task3.cancel().
Note that we have defined max_workers=2 for our ThreadPoolExecutor, and as such, by the time we call task3.cancel(), the executor should be preoccupied running tasks 1 and 2, and shouldn't yet have started task 3:
import time
import random
from concurrent.futures import ThreadPoolExecutor
def someTask(n):
print("Executing Task {}".format(n))
time.sleep(n)
print("Task {} Finished Executing".format(n))
def main():
with ThreadPoolExecutor(max_workers=2) as executor:
task1 = executor.submit(someTask, (1))
task2 = executor.submit(someTask, (2))
task3 = executor.submit(someTask, (3))
task4 = executor.submit(someTask, (4))
print(task3.cancel())
if __name__ == '__main__':
main()