Q2 Open

This module provides an easy interface for asynchronous file access

Reading and writing to files in Python is typically a blocking operation. Using q2_open instead of the built-in open provides the ability to do file operations without blocking the main even loop by wrapping each file method in a thread based executor.

There are minor differences between open and q2_open.

1) q2_open must be used as an asynchronous context manager (the async with statement). This ensures that the file gets closed after exiting the async with block.

2) The resulting file handle provided by q2_open has the same methods as the file handle from open but they are all called with await. Using the file handle from q2_open as an iterator to loop over each line requires an async for loop rather than the normal for loop.

enum q2_sdk.core.q2_open.Whence(value)[source]
Member Type:

int

Valid values are as follows:

SET = <Whence.SET: 0>
CUR = <Whence.CUR: 1>
END = <Whence.END: 2>
q2_sdk.core.q2_open.q2_open(path, mode='r')[source]

Analogous to open() but provides non-blocking async operations.

Usage:

async def example_func(path: Path) -> str:
    contents = ""
    async with q2_open(path) as file_handle:
        async for line in file_handle:
            if line.startswith("#"):
                contents = line
                break
    return contents
Return type:

AsyncGenerator[AsyncFile, None]