0

Unlike subprocess.Popen, multiprocessing.Process doesn't have a send_signal method. Why? Is there a recommended way to send signals like SIGINT to multiprocessing.Process? Should I use os.kill() for that purpose? Thanks in advance.

Rais
  • 3
  • 1

1 Answers1

0

Your first question makes total sense.
I think that's because multiprocessing and subprocess libraries have different design goals (as explained by this answer) : the former is for making multiple Python scripts cooperate over different CPUs for achieving a common task, while the latter is for integrating external programs in your Python program. Because IPC (inter-process communication) is far easier between cooperating Python multiprocesses (there are queues and pipes, you can pass Python objects as arguments, ...) than with an external program which we can only assume to adhere to the OS interface (textual stdin/stdout, should handle signals correctly, ...).
The default way to communicate with another multiprocess is thus not an OS signal, so that it was not considered useful to integrate it.
Also remember that (C)Python is OpenSource, so you could contribute this integration yourselves.

As for your second question, there already is an answer (cf How can I send a signal from a python program?), yes :

use os.kill()

Lenormju
  • 4,078
  • 2
  • 8
  • 22
  • Thanks for your answer. I came to the same conclusion a little while ago. But the part with os.kill() is something I'm worried about. I think It is another level of abstraction. Can I be sure that if I use os.kill() for multiprocessing.Process everything will be okay (if the process has associated a signal with a function)? – Rais Jul 02 '21 at 11:29
  • @Rais whether the process was started with `subprocess` or with `multiprocess`, to the OS it looks exactly the same, and the process should adhere to the same rules (handling appropriately the signals). The only problem could be around deamons, terlinated child processes, non-waited childs ... Usual programming advices apply here, nothing different. It is always just regular OS stuff. And you are right, `os.kill` is an OS-level feature, which is not the the same level than the application (business), but there is no good alternative solution that I know of. – Lenormju Jul 02 '21 at 12:32