Skip to content

Instantly share code, notes, and snippets.

@tzuryby
Created October 26, 2009 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzuryby/218642 to your computer and use it in GitHub Desktop.
Save tzuryby/218642 to your computer and use it in GitHub Desktop.
import subprocess, time
import threading
from threading import Thread
process = None
class SThread (Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__ (self, **args):
super(SThread, self).__init__()
self._stop = threading.Event()
def stop (self):
self._stop.set()
def stopped (self):
return self._stop.isSet()
def run(self):
global process
args = ["/bin/bash","./poc.sh"]
process = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)
for line in process.communicate():
if line:
print "process:", line,
if __name__ == '__main__':
print 'starting'
t = SThread()
t.start()
time.sleep(1)
print 'killing'
process.kill()
print 'stopping'
t.stop()
def run(self):
global process
args = ["sleep", "5"]
process = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)
for line in process.communicate():
if line:
print "process:", line,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment