Reputation: 49
I'm trying to run a python script that runs, with subprocess.popen (), one or more instances of another python script. At some point I need to finish them gently. These scripts execute a while loop and when ctrl-c is catched they execute a clean-up routine and terminate.
At the beginnig my idea was to send ctrl-c to those scripts with subproc.send_signal() but it doesn't work. I tryed to use subproc.terminate() or os.kill(os.kill(subproc.pid, signal.CTRL_C_EVENT)) and other solution founded on Internet but nothing works well.
How can I gently terminate my scripts? Is the way I run my scripts right? Here is my code:
processes = []
for i in range(0, capsules_number):
p = subprocess.Popen(["python.exe ", "Capsula.py ", str(i)])
processes.append(p)
input("Press any key to terminate programs")
for p in processes:
# Terminate process
processes.remove(p)
Upvotes: 1
Views: 819
Reputation: 43495
From the subprocess
documentation:
CTRL_C_EVENT and CTRL_BREAK_EVENT can be sent to processes started with a
creationflags
parameter which includes CREATE_NEW_PROCESS_GROUP.
Upvotes: 1