Question

I have a testing script that needs to open a process (a Pyro server), do some stuff that will call the opened process for information, and when it's all done will need to close the process back down. It's all part of an automated test on a staging server.

In python 2.6 you can do this:

pyro_server = subprocess.Popen(['python', 'pyro_server.py'])
# Do stuff, making remote calls to the Pyro server on occasion
pyro_server.terminate()

Alas, I'm locked into python 2.4 here at work so I don't have access to that function. And if I just let the script end of course the server lives on. What should I be doing to close/kill that process before the script exits?

Was it helpful?

Solution

  1. Consider copying subprocess.py to your python2.4 dist-packages directory. It should just work as it's a simple wrapper around the old popen library.

  2. The Popen object terminate function does nothing more than the following:

    import os, signal

    os.kill(pid, signal.SIGKILL)

pid is the child process's process id. signal.SIGKILL is the number 9, and is a standard unix kill signal. You can see how to spawn a subprocess and get its pid in python 2.4 with the popen module here:

OTHER TIPS

@BrainCore: Note that os.kill is not available on windows in python24, check the docs.

My solution for killing a subprocess.Popen object on windows when using python24 is this:

import os, subprocess
p = subprocess.Popen(...)
res = os.system('taskkill /PID %d /F' % p.pid)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top