Python: Can you only terminate processes in a pool via pool.terminate()?

Viewed 42

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
0 Answers
Related