Fix Python – Keyboard Interrupts with python’s multiprocessing Pool


Asked By – Fragsworth

How can I handle KeyboardInterrupt events with python’s multiprocessing Pools? Here is a simple example:

from multiprocessing import Pool
from time import sleep
from sys import exit

def slowly_square(i):
    return i*i

def go():
    pool = Pool(8)
        results =, range(40))
    except KeyboardInterrupt:
        # **** THIS PART NEVER EXECUTES. ****
        print "You cancelled the program!"
    print "\nFinally, here are the results: ", results

if __name__ == "__main__":

When running the code above, the KeyboardInterrupt gets raised when I press ^C, but the process simply hangs at that point and I have to kill it externally.

I want to be able to press ^C at any time and cause all of the processes to exit gracefully.

Now we will see solution for issue: Keyboard Interrupts with python’s multiprocessing Pool


This is a Python bug. When waiting for a condition in threading.Condition.wait(), KeyboardInterrupt is never sent. Repro:

import threading
cond = threading.Condition(threading.Lock())
print "done"

The KeyboardInterrupt exception won’t be delivered until wait() returns, and it never returns, so the interrupt never happens. KeyboardInterrupt should almost certainly interrupt a condition wait.

Note that this doesn’t happen if a timeout is specified; cond.wait(1) will receive the interrupt immediately. So, a workaround is to specify a timeout. To do that, replace

    results =, range(40))


    results = pool.map_async(slowly_square, range(40)).get(9999999)

or similar.

This question is answered By – Glenn Maynard

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0