admin管理员组

文章数量:1122832

Ask please why press Ctrl + C ( KeyboardInterrupt) does not work as expected. (the process is not interrupted, continues to work)

from concurrent.futures import ThreadPoolExecutor

def sleeper(n):
    while True:
        pass


def run():
    try:
        with ThreadPoolExecutor(max_workers=10) as executor:
            results = executor.map(sleeper, list(range(120)))
    except KeyboardInterrupt:
        exit()


if __name__ == '__main__':
    run()

Ask please why press Ctrl + C ( KeyboardInterrupt) does not work as expected. (the process is not interrupted, continues to work)

from concurrent.futures import ThreadPoolExecutor

def sleeper(n):
    while True:
        pass


def run():
    try:
        with ThreadPoolExecutor(max_workers=10) as executor:
            results = executor.map(sleeper, list(range(120)))
    except KeyboardInterrupt:
        exit()


if __name__ == '__main__':
    run()
Share Improve this question edited Nov 25, 2024 at 14:59 Alex Duchnowski 5733 silver badges19 bronze badges asked Nov 22, 2024 at 17:54 VitaliyVitaliy 154 bronze badges 4
  • 1 You should tell us how it SHOULD work. – Razzer Commented Nov 22, 2024 at 18:00
  • This is testing Exception KeyboardInterrupt( Ctrl + C) only. Elso i have added time.sleep(0.1) in sleeper function, but result same..(the process is not interrupted, continues to work) – Vitaliy Commented Nov 22, 2024 at 18:05
  • What is the definition of exit? – Paul M. Commented Nov 23, 2024 at 2:40
  • I'm not very familiar with threads, but by messing around with your code a bit, I've come to the conclusion that the interpreter is receiving the interrupt signal, but it's not sending this signal to all of the threads that are running. In other words, it's masking the signal as it waits for all the processes to complete, and then the signal is porcessed. You can get a better understanding by adding print statements to various parts of your code and using time.sleep() instead of infinite loops so that your program does terminate eventually (regardless of signal processing). – Alex Duchnowski Commented Nov 23, 2024 at 8:54
Add a comment  | 

1 Answer 1

Reset to default 1

The Keyboard Interrupt signal is being received, but the program is waiting for all the threads to complete their tasks before actually processing the signal fully. Based on this SO post and this tutorial, I modified your code to create the following program, which will halt as soon as you press Ctrl+C. It will also terminate even if you don't interrupt it, so you won't have to restart any terminals or run commands to kill individual processes by hand if something goes wrong somehow:

import time
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import Process


def sleeper(n):
    print("Sleeper started for", n)
    time.sleep(0.25)
    return n


def run():
    with ThreadPoolExecutor(max_workers=10) as executor:
        results = executor.map(sleeper, list(range(500)))
        return results


if __name__ == "__main__":
    process = Process(target=run)
    process.start()
    try:
        while process.is_alive():
            time.sleep(0.1)
    except KeyboardInterrupt:
        print("Received KeyboardInterrupt")
        process.terminate()

本文标签: pythonKeyboardInterrupt doesn39t work as expectedStack Overflow