@@ -100,7 +100,9 @@ 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 = False
103
+ self .running = True
104
+ self ._calling_thread = threading .current_thread ()
105
+ self .worker .start ()
104
106
105
107
def _check_closed (self , msg = None ):
106
108
if self ._closed :
@@ -109,34 +111,23 @@ def _check_closed(self, msg=None):
109
111
def _decompress (self ):
110
112
block_size = self .block_size
111
113
block_queue = self .queue
112
- while self .running :
114
+ while self .running and self . _calling_thread . is_alive () :
113
115
try :
114
116
data = self .fileobj .read (block_size )
115
117
except Exception as e :
116
118
self .exception = e
117
119
return
118
120
if not data :
119
121
return
120
- while self .running :
122
+ while self .running and self . _calling_thread . is_alive () :
121
123
try :
122
124
block_queue .put (data , timeout = 0.05 )
123
125
break
124
126
except queue .Full :
125
127
pass
126
128
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
-
137
129
def readinto (self , b ):
138
130
self ._check_closed ()
139
- self ._start ()
140
131
result = self .buffer .readinto (b )
141
132
if result == 0 :
142
133
while True :
@@ -164,7 +155,8 @@ def tell(self) -> int:
164
155
def close (self ) -> None :
165
156
if self ._closed :
166
157
return
167
- self ._stop ()
158
+ self .running = False
159
+ self .worker .join ()
168
160
self .fileobj .close ()
169
161
if self .closefd :
170
162
self .raw .close ()
@@ -224,6 +216,7 @@ def __init__(self,
224
216
if "b" not in mode :
225
217
mode += "b"
226
218
self .lock = threading .Lock ()
219
+ self ._calling_thread = threading .current_thread ()
227
220
self .exception : Optional [Exception ] = None
228
221
self .level = level
229
222
self .previous_block = b""
@@ -261,6 +254,7 @@ def __init__(self,
261
254
self .raw , self .closefd = open_as_binary_stream (filename , mode )
262
255
self ._closed = False
263
256
self ._write_gzip_header ()
257
+ self .start ()
264
258
265
259
def _check_closed (self , msg = None ):
266
260
if self ._closed :
@@ -283,24 +277,21 @@ def _write_gzip_header(self):
283
277
self .raw .write (struct .pack (
284
278
"BBBBIBB" , magic1 , magic2 , method , flags , mtime , os , xfl ))
285
279
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 ()
280
+ def start (self ):
281
+ self .running = True
282
+ self .output_worker .start ()
283
+ for worker in self .compression_workers :
284
+ worker .start ()
292
285
293
286
def stop (self ):
294
287
"""Stop, but do not care for remaining work"""
295
- if self .running :
296
- self .running = False
297
- for worker in self .compression_workers :
298
- worker .join ()
299
- self .output_worker .join ()
288
+ self .running = False
289
+ for worker in self .compression_workers :
290
+ worker .join ()
291
+ self .output_worker .join ()
300
292
301
293
def write (self , b ) -> int :
302
294
self ._check_closed ()
303
- self ._start ()
304
295
with self .lock :
305
296
if self .exception :
306
297
raise self .exception
@@ -360,7 +351,7 @@ def _compress(self, index: int):
360
351
in_queue = self .input_queues [index ]
361
352
out_queue = self .output_queues [index ]
362
353
compressor : zlib_ng ._ParallelCompress = self .compressors [index ]
363
- while True :
354
+ while self . _calling_thread . is_alive () :
364
355
try :
365
356
data , zdict = in_queue .get (timeout = 0.05 )
366
357
except queue .Empty :
@@ -383,7 +374,7 @@ def _write(self):
383
374
fp = self .raw
384
375
total_crc = 0
385
376
size = 0
386
- while True :
377
+ while self . _calling_thread . is_alive () :
387
378
out_index = index % self .threads
388
379
output_queue = output_queues [out_index ]
389
380
try :
@@ -408,7 +399,7 @@ def _compress_and_write(self):
408
399
size = 0
409
400
in_queue = self .input_queues [0 ]
410
401
compressor = self .compressors [0 ]
411
- while True :
402
+ while self . _calling_thread . is_alive () :
412
403
try :
413
404
data , zdict = in_queue .get (timeout = 0.05 )
414
405
except queue .Empty :
0 commit comments