Add asyncio async file I/O (open_file, AsyncFile, wrap_file, Path)#145358
Closed
kovan wants to merge 1 commit intopython:mainfrom
Closed
Add asyncio async file I/O (open_file, AsyncFile, wrap_file, Path)#145358kovan wants to merge 1 commit intopython:mainfrom
kovan wants to merge 1 commit intopython:mainfrom
Conversation
…Path Adds thread-delegated async file I/O to asyncio, following proven Trio/anyio patterns. open_file() and AsyncFile wrap synchronous file objects with async methods via asyncio.to_thread(). Path wraps pathlib.Path with async I/O methods and sync property passthrough. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
|
New features always require an issue, and features of this magnitude should be discussed on DPO first. |
Author
|
It was discussed, see the link on the description. |
Author
|
Also issue created. |
Member
A discussion thread simply existing isn't nearly enough to warrant a new feature. You need a core dev (preferably an asyncio maintainer in this case) to accept new feature requests as valid. |
Author
|
Ok, there is, DPO thread, issue, PRs, everything, I can't do more. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
asyncio.open_file()— async equivalent of builtinopen(), delegates to a thread viaasyncio.to_thread()asyncio.AsyncFile— async wrapper around synchronous file objects with explicit async I/O methods (read,write,seek,tell,flush,truncate,readline,readlines,writelines,read1,peek,aclose) plus__getattr__passthrough for sync attributes,async with, andasync for line in fsupportasyncio.wrap_file()— wraps an already-open file-like object asAsyncFileasyncio.Path— async wrapper aroundpathlib.Pathwith async I/O methods and sync property/method passthroughThis follows the proven Trio/anyio pattern of delegating blocking file operations to threads. All async methods use
asyncio.to_thread()for thread delegation, reusing existing asyncio infrastructure and propagating contextvars.Part of the proposal to adopt proven anyio/Trio patterns natively into asyncio (Tier 2, item 2.3). Tracking issue: #145370.
Design decisions
__getattr__dispatch) — clearer for stdlib__getattr__passthrough for sync attributes (name,mode,closed, etc.) — avoids listing dozens of propertiesPath(like anyio) rather than subclassingpathlib.Path— avoids coupling to pathlib internalsiterdir()/glob()/rglob()/walk()— avoidsStopIteration-in-Future issues_async_method/_async_method_pathhelpers to reduce boilerplate inPath(like Trio)Test plan
AsyncFileandPath— all passing./python -m test test_asyncio.test_fileio -vpasses