Skip to content

Add asyncio async file I/O (open_file, AsyncFile, wrap_file, Path)#145358

Closed
kovan wants to merge 1 commit intopython:mainfrom
kovan:asyncio-file-io
Closed

Add asyncio async file I/O (open_file, AsyncFile, wrap_file, Path)#145358
kovan wants to merge 1 commit intopython:mainfrom
kovan:asyncio-file-io

Conversation

@kovan
Copy link

@kovan kovan commented Feb 28, 2026

Summary

  • Adds asyncio.open_file() — async equivalent of builtin open(), delegates to a thread via asyncio.to_thread()
  • Adds 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, and async for line in f support
  • Adds asyncio.wrap_file() — wraps an already-open file-like object as AsyncFile
  • Adds asyncio.Path — async wrapper around pathlib.Path with async I/O methods and sync property/method passthrough

This 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

  • Explicit async methods for I/O operations (not dynamic __getattr__ dispatch) — clearer for stdlib
  • __getattr__ passthrough for sync attributes (name, mode, closed, etc.) — avoids listing dozens of properties
  • Wrapper pattern for Path (like anyio) rather than subclassing pathlib.Path — avoids coupling to pathlib internals
  • Sentinel-based async iterators for iterdir()/glob()/rglob()/walk() — avoids StopIteration-in-Future issues
  • _async_method/_async_method_path helpers to reduce boilerplate in Path (like Trio)

Test plan

  • 55 tests covering AsyncFile and Path — all passing
  • Verify ./python -m test test_asyncio.test_fileio -v passes

…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>
@ZeroIntensity
Copy link
Member

New features always require an issue, and features of this magnitude should be discussed on DPO first.

@kovan
Copy link
Author

kovan commented Feb 28, 2026

It was discussed, see the link on the description.

@kovan
Copy link
Author

kovan commented Feb 28, 2026

Also issue created.

@ZeroIntensity
Copy link
Member

It was discussed, see the link on the description.

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.

@kovan
Copy link
Author

kovan commented Feb 28, 2026

Ok, there is, DPO thread, issue, PRs, everything, I can't do more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants