I have a thread which calls Popen to get a string back from a command line utility. This command line function does not return until some very, very laggy network data arrives. Sometimes it could take minutes, other times under a second.
If the user wants, they can can cancel waiting for this data. In this case, what is the right way to stop the thread?
class CommThread( threading.Thread ):
def __init__(self):
self.stdout = None
self.stderr = None
self.command = None
threading.Thread.__init__(self)
def run(self):
if self.command is not None:
p = Popen( self.command.split(), shell=False, stdout=PIPE, stderr=PIPE)
self.stdout, self.stderr = p.communicate()
Use Popen.terminate() here’s the document http://docs.python.org/library/subprocess.html
you code should be like this:
you can call CommThread.stop() in other code blocks