def __init__(self, source, target, buffer_size, chunk_size): super(InputStreamChunker, self).__init__() self.daemon = True # die die die. self.source = source self.target = target self.chunk_count_max = int(buffer_size / chunk_size) + 1 self.chunk_size = chunk_size self.data_added = Event() self.data_added.clear() self.keep_reading = Event() self.keep_reading.set() self.EOF = Event() self.EOF.clear() self.go = Event() self.go.set()
class InputStreamChunker(Thread): def __init__(self, source, target, buffer_size, chunk_size): super(InputStreamChunker, self).__init__() self.daemon = True # die die die. self.source = source self.target = target self.chunk_count_max = int(buffer_size / chunk_size) + 1 self.chunk_size = chunk_size self.data_added = Event() self.data_added.clear() self.keep_reading = Event() self.keep_reading.set() self.EOF = Event() self.EOF.clear() self.go = Event() self.go.set() def stop(self): self.go.clear() self.EOF.set() try: # this is not proper, but is done to force the reader thread let # go of the input because, if successful, .close() will send EOF # down the pipe. self.source.close() except: pass def run(self): s = self.source t = self.target cs = self.chunk_size ccm = self.chunk_count_max kr = self.keep_reading da = self.data_added go = self.go try: b = s.read(cs) except ValueError: b = '' while b and go.is_set(): if len(t) > ccm: kr.clear() kr.wait(2) # # this only works on 2.7.x and up # if not kr.wait(10): # raise Exception("Timed out while waiting for input to be read.") # instead we'll use this if len(t) > ccm + 3: raise IOError( "Timed out while waiting for input from subprocess.") t.append(b) da.set() b = s.read(cs) self.EOF.set() da.set() # for cases when done but there was no input.
class InputStreamChunker(Thread): def __init__(self, source, target, buffer_size, chunk_size): super(InputStreamChunker, self).__init__() self.daemon = True # die die die. self.source = source self.target = target self.chunk_count_max = int(buffer_size / chunk_size) + 1 self.chunk_size = chunk_size self.data_added = Event() self.data_added.clear() self.keep_reading = Event() self.keep_reading.set() self.EOF = Event() self.EOF.clear() self.go = Event() self.go.set() def stop(self): self.go.clear() self.EOF.set() try: # this is not proper, but is done to force the reader thread let # go of the input because, if successful, .close() will send EOF # down the pipe. self.source.close() except: pass def run(self): s = self.source t = self.target cs = self.chunk_size ccm = self.chunk_count_max kr = self.keep_reading da = self.data_added go = self.go b = s.read(cs) while b and go.is_set(): if len(t) > ccm: kr.clear() kr.wait(2) # # this only works on 2.7.x and up # if not kr.wait(10): # raise Exception("Timed out while waiting for input to be read.") # instead we'll use this if len(t) > ccm + 3: raise IOError("Timed out while waiting for input from subprocess.") t.append(b) da.set() b = s.read(cs) self.EOF.set() da.set() # for cases when done but there was no input.