klenwell information services : PythonSubprocessing

Python Subprocess and Thread Handling

return to DevPython

Basics Subprocess Example

In python interpreter
>>> import subprocess, os, signal
>>> cmd1 = ['touch', '/tmp/subprocess.txt']
>>> ps1 = subprocess.Popen(cmd1)
>>> print ps1.pid
15035
>>> ps1.poll()
0
>>> print ps1.returncode
0
>>> cmd2 = ['tail', '-f', '/tmp/subprocess.txt']
>>> ps2 = subprocess.Popen(cmd2)
>>> print ps2.pid
15399
>>> ps2.poll()
>>> print ps2.returncode
None
>>> os.kill(ps2.pid, signal.SIGKILL)
>>> ps2.poll()
-9
>>> print ps2.returncode
-9



References

http://docs.python.org/library/subprocess.html#popen-objects
http://stackoverflow.com/questions/1064335/in-python-2-5-how-do-i-kill-a-subprocess