Skip to content

Instantly share code, notes, and snippets.

@curzona
Created June 12, 2015 20:48
Show Gist options
  • Select an option

  • Save curzona/22e70178fa933b84815b to your computer and use it in GitHub Desktop.

Select an option

Save curzona/22e70178fa933b84815b to your computer and use it in GitHub Desktop.
Killing subprocesses in python
import time
import subprocess
import threading
cmd = ['timeout', '10']
p = subprocess.Popen(cmd)
p.killed = False
def timeout():
time.sleep(1)
if p.poll() is None:
p.killed = True
p.kill()
t = threading.Thread(target=timeout)
t.start()
p.wait()
print "killed", p.killed
print "returncode", p.returncode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment