2

I am trying to terminate the processes belonging to a pool. The pool processes are carrying out calculations and a stop button in a Gui should end these calculations.

It seems the simple way to do this is by calling pool.terminate(). This option isn't available to me because I don't have access to the pool variable in my scope. It was created in a file that I'd rather not edit.

I tried an approach by terminating the processes by process ID. I get the pids from a list created by active_children. But it seems that os.kill has no effect as all the processes are still there. Where did I go wrong/how can I solve this? I'd appreciate any help.

Below is a minimal, reproducable example. Also if my post indicates an obvious lack of knowledge, it's probably true and I apologize. thank you

from multiprocessing import Pool
from multiprocessing import active_children
import os, signal


if __name__ == '__main__':
    pool = Pool()
    print(active_children())

    for process in active_children():
        pid = process.pid
        os.kill(pid, signal.SIGTERM)

    print(active_children())  #same output as previous print statement
    pool.terminate()
    print(active_children())  #returns an empty list
Teldryn0
  • 21
  • 2
  • 1
    Can you modify the tasks/functions that are being run in the pool? – wwii Jul 25 '20 at 13:13
  • 1
    Do the processes with the pids given by `process.pid` show up in the operating system lists? If you're on Linux, type `ps -A` in a terminal to find out. On Windows, maybe TaskManager? Based on what I see by trying out your code, it's possible that the pool just shows you process objects that no longer have a process. – Yosef Meller Jul 25 '20 at 13:14
  • @wwii: If I ask for permission I could. Basically I am creating a GUI for someone elses program and I'm trying to keep his code untouched. If I can't find a solution here I'm going to modify it so that I can use pool.terminate(). I hope I understood your question correctly. – Teldryn0 Jul 25 '20 at 13:18
  • @Yosef Meller: Unfortnately all the processes are still up and feeding the CPU according to TaskManager. – Teldryn0 Jul 25 '20 at 13:23
  • 1
    One option would be to have the tasks periodically look for a signal to stop then activate that signal in the gui. But if you have no way of communicating with the processes and constraints on modifying existing files this won't work, there are a number of similar Q&A's here on SO but they all rely on having *control* of the processes/tasks. Or ask your friend to make the pool a module level variable that you can use when you import the module. – wwii Jul 25 '20 at 13:24
  • @wwii: Thank you for your help. I'll likely just do that because it's easier than to keep browsing forums and threads. I'm still a bit surprised that terminating by pid didn't work. – Teldryn0 Jul 25 '20 at 13:56
  • Did you run across - https://stackoverflow.com/a/17858114/2823755? – wwii Jul 25 '20 at 14:08

0 Answers0