Skip to content

Commit 9cdfc65

Browse files
committed
fix(stream_io): Close finished streams, add context manager methods
1 parent ec12b17 commit 9cdfc65

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

tensorizer/stream_io.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ def __init__(self,
149149
self._end = end
150150
self.closed = False
151151

152+
def __enter__(self):
153+
return self
154+
155+
def __exit__(self, exc_type, exc_val, exc_tb):
156+
self.close()
157+
152158
def __del__(self):
153159
self.close()
154160

@@ -219,6 +225,10 @@ def close(self):
219225
self._curl.stdout.close()
220226
self._curl.terminate()
221227
self._curl.wait()
228+
else:
229+
# stdout is normally closed by the Popen.communicate() method,
230+
# which we skip in favour of Popen.stdout.read()
231+
self._curl.stdout.close()
222232
self._curl = None
223233

224234
def readline(self):

0 commit comments

Comments
 (0)