2

Possible Duplicate:
Keyboard Interrupts with python's multiprocessing Pool

Python's multiprocessing module has something called Pool http://docs.python.org/library/multiprocessing.html#module-multiprocessing.pool

While a pool of processes is operating, I can't get the script to terminate using KeyboardInterrupt i.e Ctrl + c. The pool spawns new processes and the only way to get out is ctrl + z followed by killing them manually.

Here is the script that I tried to test it on:

import multiprocessing
import random
import time

def sometask(arg):
    #do something nasty to arg
    time.sleep(arg)
    return random.randint(0,arg)

if __name__=="__main__":
    pool = multiprocessing.Pool(processes=4)
    print pool.map(sometask, range(10))

My main script tries to do something that is much more time consuming than time.sleep() and every time I try to test run it, I have to wait for it to finish or kill it manually by first finding the id of the processes it spawned. Please suggest a work around.

Community
  • 1
  • 1
Anuvrat Parashar
  • 2,960
  • 5
  • 28
  • 55

1 Answers1

1

I faced this problem too. A possible work around (a dirty one) can be spawn another process and pass that process the PID of the main script. Let that process kill the main script. I have tried this and it works fine for me.

vaidik
  • 2,191
  • 1
  • 16
  • 22