Skip to content

Commit c7d0a01

Browse files
committed
Fix a few errors in the threadedgzipwriter
1 parent 3453a7a commit c7d0a01

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/isal/igzip_threaded.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ def close(self) -> None:
108108
self.worker.join()
109109
self.fileobj.close()
110110

111+
def __enter__(self):
112+
return self
113+
114+
def __exit__(self, exc_type, exc_val, exc_tb):
115+
self.close()
116+
111117

112118
class ThreadedGzipWriter(io.RawIOBase):
113119
def __init__(self,
@@ -130,7 +136,7 @@ def __init__(self,
130136
self.running = False
131137
self._size = 0
132138
self.crc_worker = threading.Thread(target=self._calculate_crc)
133-
self.output_worker = threading.Thread(target=self.write)
139+
self.output_worker = threading.Thread(target=self._write)
134140
self.compression_workers = [
135141
threading.Thread(target=self._compress, args=(i,))
136142
for i in range(threads)
@@ -194,6 +200,8 @@ def close(self) -> None:
194200
self.flush()
195201
self.crc_queue.join()
196202
self.stop_immediately()
203+
# Write an empty deflate block with a lost block marker.
204+
self.raw.write(isal_zlib.compress(b"", wbits=-15))
197205
trailer = struct.pack("<II", self._crc, self._size & 0xFFFFFFFF)
198206
self.raw.write(trailer)
199207
self.raw.flush()
@@ -238,11 +246,21 @@ def _write(self):
238246
output_queues = self.output_queues
239247
fp = self.raw
240248
while self.running:
241-
output_queue = output_queues[index]
249+
out_index = index % self.threads
250+
output_queue = output_queues[out_index]
242251
try:
243252
data = output_queue.get(timeout=0.05)
244253
except queue.Empty:
245254
continue
246255
fp.write(data)
247256
output_queue.task_done()
248257
index += 1
258+
259+
def writable(self) -> bool:
260+
return True
261+
262+
def __enter__(self):
263+
return self
264+
265+
def __exit__(self, exc_type, exc_val, exc_tb):
266+
self.close()

0 commit comments

Comments
 (0)