duck.utils.fileio¶
FileIOStream module.
Provides both synchronous and asynchronous file streaming interfaces.
Ideal for efficient reading of large files using chunked reads and supporting
standard seek, tell, and close operations in both environments.
Methods that do not need to be async: Even in async context, the below methods don’t necessarily need to be async:
open- Time complexity is O(1)seek- Time complexity is O(1)tell- Time complexity is O(1)
In async context, only read, write, and close need to be asynchronous.
Module Contents¶
Classes¶
Asynchronous file streaming class. |
|
Synchronous file streaming class that mimics |
Functions¶
Converts file_io_stream to async file io stream if not already async. |
API¶
- class duck.utils.fileio.AsyncFileIOStream(*args, **kwargs)[source]¶
Bases:
duck.utils.fileio.FileIOStreamAsynchronous file streaming class.
Provides async-compatible methods for reading and writing files in a non-blocking way using threads via
asyncio.to_thread.Notes:
This implementation is compatible with context managers.
Initialization
Initialize the FileIOStream object.
- Parameters:
filepath – Path to the file to be streamed.
chunk_size – Maximum number of bytes to read or write at once. Default is 2MB.
open_now – Whether to open the file immediately. Defaults to False.
mode – File open mode (default: ‘rb’).
- class duck.utils.fileio.FileIOStream(filepath: str, chunk_size: int = 2 * 1024 * 1024, open_now: bool = False, mode: str = 'rb')[source]¶
Bases:
io.IOBaseSynchronous file streaming class that mimics
io.IOBase.This class provides an interface to stream file contents using standard file operations such as
read,write,seek,tell, andclose. It is optimized for chunked reading of large files and is designed to be used strictly in synchronous contexts.Initialization
Initialize the FileIOStream object.
- Parameters:
filepath – Path to the file to be streamed.
chunk_size – Maximum number of bytes to read or write at once. Default is 2MB.
open_now – Whether to open the file immediately. Defaults to False.
mode – File open mode (default: ‘rb’).
- __del__()[source]¶
Ensure the file is closed on delete else it raises a RuntimeError if file not closed.
- __slots__¶
None
- duck.utils.fileio.to_async_fileio_stream(fileio_stream: FileIOStream) AsyncFileIOStream[source]¶
Converts file_io_stream to async file io stream if not already async.