1

I've got a python script which maps in multiprocessing a function with a list of input.

import multiprocessing as mp

L = [(x1, y1), (x2, y2), ...]

with mp.Pool(processes = N) as p:
    p.starmap(f, L)

I would like to have a way to shutdown properly the program.

The idea would be to have a key to hit, "Q" for instance, or "Ctrl+Q" that would then stop all the processes at the end of the current iteration. In single threading program, I usually interrupt with Ctrl+C.

Can this be done in python?

Thanks :)

N.B: Python version 3.6

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • Possible duplicate of [threading ignores KeyboardInterrupt exception](https://stackoverflow.com/questions/3788208/threading-ignores-keyboardinterrupt-exception) – code11 May 04 '18 at 13:16
  • @code11 That's only part of the question. I would like the keyboard shutcut to interrupt the processes AFTER finishing the on going iteration. – Mathieu May 04 '18 at 13:31
  • Have the keyboard exception trip a flag that exits the application after the loop is done. – code11 May 04 '18 at 13:33
  • @code11 One, I've got no clue about half of what your sentence means. If you got a proposition, an answer would be welcome. Second, "the loop is done", which loop? I don't feel like we got much control on the multiprocessing mapping. – Mathieu May 04 '18 at 13:35
  • I don't have a python environment on me right now, but it looks like other people have had similar problems. Catching the interrupt seems tricky: https://stackoverflow.com/q/1408356/1456253. However, after that its up to you with what the application does with it. – code11 May 04 '18 at 13:52

0 Answers0