This is what i use. It uses procfs (so you are limited to Unix like systems, will not work on macs i think) and the previously mentioned glob. It also gets the cmdline, which allows you to identify the process. For killing the process you can use os.kill(signal.SIGTERM, pid)
. For using subprocess, please check this post http://stackoverflow.com/questions/337863/python-popen-and-select-waiting-for-a-process-to-terminate-or-a-timeout/1035488#1035488
def list_processes():
"""
This function will return an iterator with the process pid/cmdline tuple
:return: pid, cmdline tuple via iterator
:rtype: iterator
>>> for procs in list_processes():
>>> print procs
('5593', '/usr/lib/mozilla/kmozillahelper')
('6353', 'pickup -l -t fifo -u')
('6640', 'kdeinit4: konsole [kdeinit]')
('6643', '/bin/bash')
('7451', '/usr/bin/python /usr/bin/ipython')
"""
for pid_path in glob.glob('/proc/[0-9]*/'):
# cmdline represents the command whith which the process was started
f = open("%s/cmdline" % pid_path)
pid = pid_path.split("/")[2] # get the PID
# we replace the \x00 to spaces to make a prettier output from kernel
cmdline = f.read().replace("\x00", " ").rstrip()
f.close()
yield (pid, cmdline)