@@ -70,6 +70,14 @@ def compress_sync(self, data: bytes) -> bytes:
70
70
return self ._compressor .compress (data )
71
71
72
72
async def compress (self , data : bytes ) -> bytes :
73
+ """Compress the data and returned the compressed bytes.
74
+
75
+ Note that flush() must be called after the last call to compress()
76
+
77
+ If the data size is large than the max_sync_chunk_size, the compression
78
+ will be done in the executor. Otherwise, the compression will be done
79
+ in the event loop.
80
+ """
73
81
async with self ._compress_lock :
74
82
# To ensure the stream is consistent in the event
75
83
# there are multiple writers, we need to lock
@@ -79,8 +87,8 @@ async def compress(self, data: bytes) -> bytes:
79
87
self ._max_sync_chunk_size is not None
80
88
and len (data ) > self ._max_sync_chunk_size
81
89
):
82
- return await asyncio .get_event_loop ().run_in_executor (
83
- self ._executor , self .compress_sync , data
90
+ return await asyncio .get_running_loop ().run_in_executor (
91
+ self ._executor , self ._compressor . compress , data
84
92
)
85
93
return self .compress_sync (data )
86
94
@@ -107,12 +115,18 @@ def decompress_sync(self, data: bytes, max_length: int = 0) -> bytes:
107
115
return self ._decompressor .decompress (data , max_length )
108
116
109
117
async def decompress (self , data : bytes , max_length : int = 0 ) -> bytes :
118
+ """Decompress the data and return the decompressed bytes.
119
+
120
+ If the data size is large than the max_sync_chunk_size, the decompression
121
+ will be done in the executor. Otherwise, the decompression will be done
122
+ in the event loop.
123
+ """
110
124
if (
111
125
self ._max_sync_chunk_size is not None
112
126
and len (data ) > self ._max_sync_chunk_size
113
127
):
114
- return await asyncio .get_event_loop ().run_in_executor (
115
- self ._executor , self .decompress_sync , data , max_length
128
+ return await asyncio .get_running_loop ().run_in_executor (
129
+ self ._executor , self ._decompressor . decompress , data , max_length
116
130
)
117
131
return self .decompress_sync (data , max_length )
118
132
0 commit comments