I know the question on terminating a subprocess call has been asked a few times, including here but, trying to follow those answers, I can't seem to get mine to exit if the script hangs (in my case I am executing a phantomjs script). For instance, if I try to load a non-existent jquery file in my phantom code, the script will hang, even though I have a timeout. Here's my code:
def kill_proc():
if p.poll() != 0:
process.kill()
p = subprocess.Popen(['phantomjs','file.js'],stdout=subprocess.PIPE)
out, phantomError = p.communicate()
t = Timer(5, kill_proc) # should kill it after 5 seconds
t.start()
p.wait()
My phantomjs script (a work in progress):
var page = require('webpage').create();
page.includeJs("http://localhost/jquery.js",function(){
phantom.exit();
});