streamed_file¶
A file-like object backed by a streaming response.
This module provides :class:StreamedFile, which wraps a streaming iterable
response into a seekable, readable file-like interface backed by a temporary
file. Data is consumed from the stream in a background thread, allowing reads
to proceed as data arrives, with proper blocking semantics for reads ahead of
the stream position.
StreamedFile
¶
A seekable, readable file-like object fed by a streaming response.
The stream is consumed by a background daemon thread into a temp file. Reads block until the requested data is available or the stream completes or the file is closed.
Example
from py_misc_utils import streamed_file as sf
# A streaming response, such as requests.get(url, stream=True).iter_content.
resp = iter([b'chunk1', b'chunk2'])
with sf.StreamedFile(resp) as f:
print(f.read(6)) # b'chunk1'
print(f.seek(0, 2), f.tell()) # 12
print(f.read(6)) # b'' at EOF - seek first to re-read
Source code in py_misc_utils/streamed_file.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
closed
property
¶
Whether the file is closed.
Returns:
| Type | Description |
|---|---|
|
True if the file has been closed. |
close()
¶
Close the file, waiting for the streaming thread to finish.
Source code in py_misc_utils/streamed_file.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
seek(pos, whence=os.SEEK_SET)
¶
Move the file position indicator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
The position to seek to. |
required | |
whence
|
The reference point (SEEK_SET, SEEK_CUR, or SEEK_END). |
SEEK_SET
|
Returns:
| Type | Description |
|---|---|
|
The new absolute offset. |
Source code in py_misc_utils/streamed_file.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
tell()
¶
Return the current file position.
Returns:
| Type | Description |
|---|---|
|
The current offset. |
Source code in py_misc_utils/streamed_file.py
136 137 138 139 140 141 142 | |
read(size=-1)
¶
Read up to size bytes from the current position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
Number of bytes to read; -1 to read all available. |
-1
|
Returns:
| Type | Description |
|---|---|
|
The bytes read. |
Source code in py_misc_utils/streamed_file.py
171 172 173 174 175 176 177 178 179 180 181 | |
read1(size=-1)
¶
Read up to size bytes (alias for read).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
Number of bytes to read. |
-1
|
Returns:
| Type | Description |
|---|---|
|
The bytes read. |
Source code in py_misc_utils/streamed_file.py
183 184 185 186 187 188 189 190 191 192 | |
peek(size=0)
¶
Return bytes from the current position without advancing it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
Maximum number of bytes to peek. |
0
|
Returns:
| Type | Description |
|---|---|
|
The peeked bytes (may be fewer than requested). |
Source code in py_misc_utils/streamed_file.py
194 195 196 197 198 199 200 201 202 203 204 205 206 | |
pread(offset, size)
¶
Read bytes at an absolute offset without changing the position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
The absolute offset to read from. |
required | |
size
|
Number of bytes to read. |
required |
Returns:
| Type | Description |
|---|---|
|
The bytes read. |
Source code in py_misc_utils/streamed_file.py
208 209 210 211 212 213 214 215 216 217 218 219 | |
flush()
¶
No-op; flushing is not applicable to this read-only file.
Source code in py_misc_utils/streamed_file.py
221 222 | |
readable()
¶
Whether the file is readable.
Returns:
| Type | Description |
|---|---|
|
True if the file is not closed. |
Source code in py_misc_utils/streamed_file.py
224 225 226 227 228 229 230 | |
seekable()
¶
Whether the file is seekable.
Returns:
| Type | Description |
|---|---|
|
True if the file is not closed. |
Source code in py_misc_utils/streamed_file.py
232 233 234 235 236 237 238 | |
writable()
¶
Whether the file is writable.
Returns:
| Type | Description |
|---|---|
|
Always False for StreamedFile. |
Source code in py_misc_utils/streamed_file.py
240 241 242 243 244 245 246 | |