Skip to content

Commit 5e41cb6

Browse files
committed
Add documentation for threaded open
1 parent e94b965 commit 5e41cb6

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

docs/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ API-documentation: igzip
124124
:members:
125125
:special-members: __init__
126126

127+
=================================
128+
API-documentation: igzip_threaded
129+
=================================
130+
131+
.. automodule:: isal.igzip_threaded
132+
:members: open
133+
127134
============================
128135
API Documentation: igzip_lib
129136
============================

src/isal/igzip_threaded.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
1+
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2+
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022
3+
# Python Software Foundation; All Rights Reserved
4+
5+
# This file is part of python-isal which is distributed under the
6+
# PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2.
7+
18
import io
29
import multiprocessing
310
import os
411
import queue
512
import struct
613
import threading
7-
from typing import BinaryIO, List, Tuple
14+
from typing import BinaryIO, List, Optional, Tuple
815

916
from . import igzip, isal_zlib
1017

1118
DEFLATE_WINDOW_SIZE = 2 ** 15
1219

1320

1421
def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
15-
encoding=None, errors=None, newline=None, *, threads=-1):
22+
encoding=None, errors=None, newline=None, *, threads=1):
23+
"""
24+
Utilize threads to read and write gzip objects and escape the GIL.
25+
Comparable to gzip.open. This method is only usable for streamed reading
26+
and writing of objects. Seeking is not supported.
27+
28+
threads == 0 will defer to igzip.open. A threads < 0 will attempt to use
29+
the number of threads in the system.
30+
31+
:param filename: str, bytes or file-like object (supporting read or write
32+
method)
33+
:param mode: the mode with which the file should be opened.
34+
:param compresslevel: Compression level, only used for gzip writers.
35+
:param encoding: Passed through to the io.TextIOWrapper, if applicable.
36+
:param errors: Passed through to the io.TextIOWrapper, if applicable.
37+
:param newline: Passed through to the io.TextIOWrapper, if applicable.
38+
:param threads: If 0 will defer to igzip.open, if < 0 will use all threads
39+
available to the system. Reading gzip can only
40+
use one thread.
41+
:return: An io.BufferedReader, io.BufferedWriter, or io.TextIOWrapper,
42+
depending on the mode.
43+
"""
1644
if threads == 0:
1745
return igzip.open(filename, mode, compresslevel, encoding, errors,
1846
newline)
@@ -32,18 +60,18 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF,
3260
else:
3361
raise TypeError("filename must be a str or bytes object, or a file")
3462
if "r" in mode:
35-
gzip_file = io.BufferedReader(ThreadedGzipReader(binary_file))
63+
gzip_file = io.BufferedReader(_ThreadedGzipReader(binary_file))
3664
else:
3765
gzip_file = io.BufferedWriter(
38-
ThreadedGzipWriter(binary_file, compresslevel, threads),
66+
_ThreadedGzipWriter(binary_file, compresslevel, threads),
3967
buffer_size=1024 * 1024
4068
)
4169
if "t" in mode:
4270
return io.TextIOWrapper(gzip_file, encoding, errors, newline)
4371
return gzip_file
4472

4573

46-
class ThreadedGzipReader(io.RawIOBase):
74+
class _ThreadedGzipReader(io.RawIOBase):
4775
def __init__(self, fp, queue_size=4, block_size=8 * 1024 * 1024):
4876
self.raw = fp
4977
self.fileobj = igzip._IGzipReader(fp, buffersize=8 * 1024 * 1024)
@@ -109,7 +137,7 @@ def close(self) -> None:
109137
self.fileobj.close()
110138

111139

112-
class ThreadedGzipWriter(io.RawIOBase):
140+
class _ThreadedGzipWriter(io.RawIOBase):
113141
"""
114142
Write a gzip file using multiple threads.
115143
@@ -145,7 +173,7 @@ def __init__(self,
145173
threads: int = 1,
146174
queue_size: int = 2):
147175
self.lock = threading.Lock()
148-
self.exception = None
176+
self.exception: Optional[Exception] = None
149177
self.raw = fp
150178
self.level = level
151179
self.previous_block = b""

0 commit comments

Comments
 (0)