End program to prevent a future death


I encountered an issue when using ThreadPoolExecutor in my program. The program appears to “freeze” when I terminate it, as if the thread is not being ended correctly. Is there a solution to this issue?

Example code:

from concurrent.futures import ThreadPoolExecutor
from time import sleep

def task():
    for i in range(3):
with ThreadPoolExecutor() as executor:
    future = executor.submit(task)
    future.cancel()  # Waits for loop of blocking task to complete
    executor.shutdown(wait=False)  # Still waits for loop in blocking task to complete

Attempting to use sys.exit() does not work, as the program still waits for the future to complete before ending.