Làm cách nào tôi có thể xử lý các sự kiện Bàn phím bị gián đoạn với Nhóm đa xử lý của python? Đây là một ví dụ đơn giản:
from multiprocessing import Pool
from time import sleep
from sys import exit
def slowly_square(i):
    sleep(1)
    return i*i
def go():
    pool = Pool(8)
    try:
        results = pool.map(slowly_square, range(40))
    except KeyboardInterrupt:
        # **** THIS PART NEVER EXECUTES. ****
        pool.terminate()
        print "You cancelled the program!"
        sys.exit(1)
    print "\nFinally, here are the results: ", results
if __name__ == "__main__":
    go()Khi chạy mã ở trên, mã KeyboardInterruptđược tăng lên khi tôi nhấn ^C, nhưng quá trình chỉ đơn giản là bị treo ở điểm đó và tôi phải giết nó bên ngoài.
Tôi muốn có thể nhấn ^Cbất cứ lúc nào và khiến tất cả các quá trình thoát ra một cách duyên dáng.