45👍
Similar to @rakslice, you can use psutil:
import signal, psutil
def kill_child_processes(parent_pid, sig=signal.SIGTERM):
try:
parent = psutil.Process(parent_pid)
except psutil.NoSuchProcess:
return
children = parent.children(recursive=True)
for process in children:
process.send_signal(sig)
9👍
Since you appear to be using Unix, you can use a quick ps
command to get the details of the child processes, like I did here (this is Linux-specific):
import subprocess, os, signal
def kill_child_processes(parent_pid, sig=signal.SIGTERM):
ps_command = subprocess.Popen("ps -o pid --ppid %d --noheaders" % parent_pid, shell=True, stdout=subprocess.PIPE)
ps_output = ps_command.stdout.read()
retcode = ps_command.wait()
assert retcode == 0, "ps command returned %d" % retcode
for pid_str in ps_output.split("\n")[:-1]:
os.kill(int(pid_str), sig)
- [Django]-What is the canonical way to find out if a Django model is saved to db?
- [Django]-Django "xxxxxx Object" display customization in admin action sidebar
- [Django]-Django templates folders
2👍
For your example you may use the subprocess
package. By default it executes the command without shell (like os.system()
) and provides a PID:
from subprocess import Popen
p = Popen('iostat 2 > a.txt', shell=True)
processId = p.pid
p.communicate() # to wait until the end
The Popen
also provides ability to connect to standard input and output of the process.
note: before using shell=True
be aware of the security considerations.
- [Django]-Django Admin – Overriding the widget of a custom form field
- [Django]-Django admin action without selecting objects
- [Django]-How to format time in django-rest-framework's serializer?
1👍
I think with the multiprocess module you might be out of luck since you are really forking python directly and are given that Process object instead of the process you are interested in at the bottom of the process tree.
An alternative way, but perhaps not optimal way, to get that pid is to use the psutil module to look it up using the pid obtained from your Process object. Psutil, however, is system dependent and will need to be installed separately on each of your target platforms.
Note: I’m not currently at a machine I typically work from, so I can’t provide working code nor play around to find a better option, but will edit this answer when I can to show how you might be able to do this.
- [Django]-Django-rest-framework http put failing with 415 on django 1.5
- [Django]-Django won't refresh staticfiles
- [Django]-Django – Model graphic representation (ERD)
1👍
[me@localhost ~]$ echo $$
30399
[me@localhost ~]$ cat iostat.py
#!/usr/bin/env python3.4
import multiprocessing
import os
d = multiprocessing.Process(target=os.system,args=('iostat 2 > a.txt',))
d.start()
[me@localhost ~]$ ./iostat.py &
[1] 31068
[me@localhost ~]$ watch -n 3 'pstree -p 30399'
[me@localhost ~]$
- [Django]-Using a UUID as a primary key in Django models (generic relations impact)
- [Django]-How do you log server errors on django sites
- [Django]-Django createview how to get the object that is created