@@ -100,8 +100,7 @@ def __init__(self, filename, queue_size=2, block_size=1024 * 1024):
100
100
self .block_size = block_size
101
101
self .worker = threading .Thread (target = self ._decompress )
102
102
self ._closed = False
103
- self .running = True
104
- self .worker .start ()
103
+ self .running = False
105
104
106
105
def _check_closed (self , msg = None ):
107
106
if self ._closed :
@@ -125,8 +124,19 @@ def _decompress(self):
125
124
except queue .Full :
126
125
pass
127
126
127
+ def _start (self ):
128
+ if not self .running :
129
+ self .running = True
130
+ self .worker .start ()
131
+
132
+ def _stop (self ):
133
+ if self .running :
134
+ self .running = False
135
+ self .worker .join ()
136
+
128
137
def readinto (self , b ):
129
138
self ._check_closed ()
139
+ self ._start ()
130
140
result = self .buffer .readinto (b )
131
141
if result == 0 :
132
142
while True :
@@ -154,8 +164,7 @@ def tell(self) -> int:
154
164
def close (self ) -> None :
155
165
if self ._closed :
156
166
return
157
- self .running = False
158
- self .worker .join ()
167
+ self ._stop ()
159
168
self .fileobj .close ()
160
169
if self .closefd :
161
170
self .raw .close ()
@@ -252,7 +261,6 @@ def __init__(self,
252
261
self .raw , self .closefd = open_as_binary_stream (filename , mode )
253
262
self ._closed = False
254
263
self ._write_gzip_header ()
255
- self .start ()
256
264
257
265
def _check_closed (self , msg = None ):
258
266
if self ._closed :
@@ -275,21 +283,24 @@ def _write_gzip_header(self):
275
283
self .raw .write (struct .pack (
276
284
"BBBBIBB" , magic1 , magic2 , method , flags , mtime , os , xfl ))
277
285
278
- def start (self ):
279
- self .running = True
280
- self .output_worker .start ()
281
- for worker in self .compression_workers :
282
- worker .start ()
286
+ def _start (self ):
287
+ if not self .running :
288
+ self .running = True
289
+ self .output_worker .start ()
290
+ for worker in self .compression_workers :
291
+ worker .start ()
283
292
284
293
def stop (self ):
285
294
"""Stop, but do not care for remaining work"""
286
- self .running = False
287
- for worker in self .compression_workers :
288
- worker .join ()
289
- self .output_worker .join ()
295
+ if self .running :
296
+ self .running = False
297
+ for worker in self .compression_workers :
298
+ worker .join ()
299
+ self .output_worker .join ()
290
300
291
301
def write (self , b ) -> int :
292
302
self ._check_closed ()
303
+ self ._start ()
293
304
with self .lock :
294
305
if self .exception :
295
306
raise self .exception
0 commit comments