Created
October 26, 2009 13:37
-
-
Save zuriby/218642 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sleep 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment