Skip to main content

Cross-platform async file API for Python. Blazing fast like Shameimaru Aya.

Project description


ayafileio

License Python Version Platform Version

当前是英文 | chinese version

"The fastest file I/O in Gensokyo, swift as the Wind God Maiden."
— Aya Shameimaru, always flying at full speed

Cross-platform asynchronous file I/O library using native async I/O where available.
Windows leverages IOCP (I/O Completion Ports), Linux uses io_uring (kernel 5.1+), and macOS uses Dispatch I/O (GCD) for truly non-blocking file operations.

changes

see -> CHANGES

🏆 The Only True Async on All Three Major Platforms

Platform Backend True Async Description
Windows IOCP NT kernel native I/O Completion Ports
Linux io_uring Next-gen async I/O (kernel 5.1+)
macOS Dispatch I/O GCD kernel-level async I/O

ayafileio is the only Python library providing true async file I/O on Windows, Linux, and macOS.

📸 Key Features

Feature Description
🍃 Zero thread overhead No background threads on true async platforms
📰 Kernel-level completion IOCP / io_uring / Dispatch I/O direct to kernel
High concurrency Handles thousands of concurrent file operations
🎴 Familiar API aiofiles-compatible, supports async/await
📖 Text & binary support Automatic encoding/decoding in text modes
🔧 Unified configuration Runtime tunable parameters for all backends
🌍 Cross-platform Windows, Linux, and macOS
🐍 Latest Python Supports 3.10, 3.11, 3.12, 3.13, 3.14

🛠️ Installation

pip install ayafileio

System requirements:

  • Python 3.10+
  • Windows 7+ / Linux (kernel 5.1+ for io_uring) / macOS 10.10+
  • No external dependencies, precompiled wheels available

🚀 Quick Start

import asyncio
import ayafileio

async def main():
    # Write to a file — fast as the wind
    async with ayafileio.open("example.txt", "w") as f:
        await f.write("Hello, async world!\n")

    # Read with automatic decoding
    async with ayafileio.open("example.txt", "r", encoding="utf-8") as f:
        content = await f.read()
        print(content)

    # Binary operations
    async with ayafileio.open("data.bin", "rb") as f:
        data = await f.read(1024)
        await f.seek(0, 0)

asyncio.run(main())

⚡ Performance Best Practice

ayafileio's file open/close overhead is already in the microsecond range, but for maximum performance, avoid reopening the same file in a loop.

# ❌ DO NOT DO THIS: repeated open/close in a loop
for i in range(10000):
    async with ayafileio.open("data.bin", "rb") as f:
        data = await f.read()

# ✅ DO THIS: open once, operate many times
async with ayafileio.open("data.bin", "rb") as f:
    for i in range(10000):
        await f.seek(0)
        data = await f.read()

The latter is ~6x faster — it eliminates 9999 unnecessary coroutine scheduling round-trips.

🔍 Backend Information

Check which backend is currently in use:

import ayafileio

info = ayafileio.get_backend_info()
print(info)
# Windows: {'platform': 'windows', 'backend': 'iocp', 'is_truly_async': True}
# Linux:   {'platform': 'linux', 'backend': 'io_uring', 'is_truly_async': True}
# macOS:   {'platform': 'macos', 'backend': 'dispatch_io', 'is_truly_async': True}

⚙️ Unified Configuration

ayafileio provides a unified configuration system that allows runtime tuning:

import ayafileio

# View current configuration
config = ayafileio.get_config()
print(config)

# Update configuration
ayafileio.configure({
    "io_worker_count": 8,
    "buffer_size": 131072,      # 128KB buffer
    "close_timeout_ms": 2000,
})

# Reset to defaults
ayafileio.reset_config()

Configuration Options

Option Default Description
handle_pool_max_per_key 64 Max cached handles per file (Windows)
handle_pool_max_total 2048 Max total cached handles (Windows)
io_worker_count 0 IO worker threads, 0=auto
buffer_pool_max 512 Max cached buffers
buffer_size 65536 Buffer size in bytes
close_timeout_ms 4000 Close timeout for pending I/O (ms)
io_uring_queue_depth 256 io_uring queue depth (Linux)
io_uring_sqpoll False Enable SQPOLL mode (Linux)

📚 API Reference

AsyncFile class

class AsyncFile(Generic[T]):
    def __init__(
        self, path: str | Path, mode: str = "rb",
        encoding: str | None = None,
        newline: str | None = None,
        errors: str | None = None
    ): ...

    # 读取
    async def read(self, size: int = -1) -> T: ...
    async def readline() -> T: ...
    async def readlines(hint: int = -1) -> list[T]: ...
    async def readall() -> T: ...                        # read(-1) 别名
    async def readinto(buf: bytearray | memoryview) -> int: ...  # 零拷贝 [仅二进制]

    # 写入
    async def write(self, data: str | bytes) -> int: ...
    async def writelines(lines) -> None: ...              # 批量写入

    # 位置
    async def seek(self, offset: int, whence: int = 0) -> int: ...
    async def tell() -> int: ...
    async def truncate(size: int) -> None: ...

    # 控制
    async def flush(self) -> None: ...
    async def close(self) -> None: ...

    # 属性
    @property
    def closed(self) -> bool: ...
    @property
    def name(self) -> str: ...
    @property
    def mode(self) -> str: ...

    # 状态
    def readable() -> bool: ...
    def writable() -> bool: ...
    def seekable() -> bool: ...
    def fileno() -> int: ...
    def isatty() -> bool: ...

    # 迭代器
    def __aiter__(self) -> AsyncFile[T]: ...
    async def __anext__(self) -> T: ...

Supported Modes

Mode Description
"r", "rb" Read (text/binary)
"w", "wb" Write (text/binary)
"a", "ab" Append (text/binary)
"x", "xb" Exclusive create (text/binary)
+ added Read/write combinations

Configuration Functions

def configure(options: dict) -> None: ...      # Unified configuration
def get_config() -> dict: ...                   # Get current configuration
def reset_config() -> None: ...                 # Reset to defaults
def get_backend_info() -> dict: ...             # Get backend information

🧪 Performance Comparison

Simulating Crawlee's Dataset append pattern (5,000 records, 50 concurrent):

Platform ayafileio aiofiles Speedup
Windows (NVMe SSD) 41,336 items/s 9,658 items/s 4.28x
Linux (NVMe SSD) 17,688 items/s 11,455 items/s 1.54x
macOS (NVMe SSD) 29,837 items/s 25,522 items/s 1.17x
Windows (6yr old HDD) 20,251 items/s 13,011 items/s 1.56x

Key findings:

  • On Windows enterprise SSD, ayafileio achieves 42x lower P99 latency (0.044ms vs 1.854ms)
  • aiofiles shows 96.7% jitter under load; ayafileio only 16.2%
  • Even on degraded hardware, ayafileio maintains predictable performance

Test environment: Windows 10/11, Ubuntu 22.04, macOS 14; GitHub Actions enterprise NVMe SSD

🤝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Add tests
  4. Ensure benchmarks pass
  5. Open a pull request

📄 License

MIT License — see LICENSE for details.


"Slow is a crime, right?"
— Aya Shameimaru, editor-in-chief of Bunbunmaru News


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ayafileio-1.1.1-cp314-cp314-win_amd64.whl (91.6 kB view details)

Uploaded CPython 3.14Windows x86-64

ayafileio-1.1.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (112.5 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ayafileio-1.1.1-cp314-cp314-macosx_15_0_arm64.whl (77.0 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

ayafileio-1.1.1-cp313-cp313-win_amd64.whl (89.2 kB view details)

Uploaded CPython 3.13Windows x86-64

ayafileio-1.1.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (112.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ayafileio-1.1.1-cp313-cp313-macosx_15_0_arm64.whl (77.0 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

ayafileio-1.1.1-cp312-cp312-win_amd64.whl (89.3 kB view details)

Uploaded CPython 3.12Windows x86-64

ayafileio-1.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (112.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ayafileio-1.1.1-cp312-cp312-macosx_15_0_arm64.whl (77.1 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

ayafileio-1.1.1-cp311-cp311-win_amd64.whl (90.1 kB view details)

Uploaded CPython 3.11Windows x86-64

ayafileio-1.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (113.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ayafileio-1.1.1-cp311-cp311-macosx_15_0_arm64.whl (78.0 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

ayafileio-1.1.1-cp310-cp310-win_amd64.whl (90.3 kB view details)

Uploaded CPython 3.10Windows x86-64

ayafileio-1.1.1-cp310-cp310-manylinux_2_28_x86_64.whl (113.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

ayafileio-1.1.1-cp310-cp310-macosx_15_0_arm64.whl (78.1 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file ayafileio-1.1.1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: ayafileio-1.1.1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 91.6 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ayafileio-1.1.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 8e40bb80bf93ff1198cfb35f31cd70a8cd5dd864c09ae6f25b97394a814835c9
MD5 761d073cea85856639963abe0a3010ed
BLAKE2b-256 6b1cc3e9385329e351ad9df2d6f1fbd205cb54a7c2eb8d37fd00611b02aa84c7

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7c205b6ff6ad3e30eb1aa71a49d32c3fe0fea8a3bf48fb2b25c69bdb47204a90
MD5 a6570928b9dfb85b58b9e291518576e6
BLAKE2b-256 9af2922ad2d4949593c23d0175a41533c8c6fbdb5996a6a3a47cd3fafb77c920

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d3be48bb65399eb70a1b6405751cc9ec5db666cf64d6ad73225eb338c19796ee
MD5 cde6d22eee3f7fe30e2129f75f1858e2
BLAKE2b-256 e8da599e236ff5262b8dde7f061c26594071a5036ade3d45f56bd5087816cb67

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: ayafileio-1.1.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 89.2 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ayafileio-1.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 88b5e31c09384b38f7fb0c47d6e5b6376a974545124eb4aedfbdf1b4b798fe94
MD5 d055e5d531378485a0fa03a4c6d25bdc
BLAKE2b-256 03ce3380334a868cd173b98df8b2f6f5c05770497fe418729b7938b040ad751f

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6c16f97816ce1ed403b7b74edfc2b38f721f1e60b47f152d8b1508e9e150351f
MD5 ee6415ef5acb43a8ec0b90dd3f4ab4df
BLAKE2b-256 5123fbd65186cf4b6349d70da1516bea6c6fe0a351ed1d4848e9038f9ae1b426

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 16ea57d9121e5f8cfd789361669e9c406132e6fa532461bdf59b2c8f3c6d3728
MD5 e8ca50a3d91c78bb61239c7faf0d4817
BLAKE2b-256 2bfed86b1751d2398c7f31c0419afd5aefe325c1332a718f7fe53cbbdec4c2b1

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: ayafileio-1.1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 89.3 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ayafileio-1.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 637f98c041422bc380cb99adf62d12c72fe44f74f2fc64c15defe23ece31e2b8
MD5 a357d429ab3308f5d1e284552d90032d
BLAKE2b-256 81555c8035e81ce31fdba8a92f69b2a9b80607b4f386648fee68dd57b5455b73

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7350712cf793f3492b12916c0fcd38763f108779c79293995e568e95adfeb8e1
MD5 1c1b50905719a517fe392813d81b69ef
BLAKE2b-256 9264bdc70ea3f838a4dc0334dc54467ecacc203e64a63c6771541341b5403219

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ddd09673d430badc6c54a14f8176e15a83bc8db804b5dd2638a8cce8e49df05f
MD5 3b8d59ab8e6baea77ebb365cac84fd70
BLAKE2b-256 9ab7c03858f65eac8886a1468b8b77f91dae6dc4b4402dbaaf1fa9873d16a5cb

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: ayafileio-1.1.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 90.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ayafileio-1.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 68b5ecc74bde9eb31da4997713be7b6322f520cee432ce58cf35cd492f7c6725
MD5 d03d4656e1c4b249c471334f5adf3b5c
BLAKE2b-256 56bac6b9729c6f7471eb923b77e3ecbe20307da97f27407e789f5ed497036220

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 51ae197d00bdd1189c771273e19a5db34f1e2d0f4f49d8dcc0bfbbf5654a68a2
MD5 b3029ba2078cf50d366aebeac4f838bc
BLAKE2b-256 afb275a28a54f4265c94e4cd68ac2364f871a9d50b22f8d76559976c0d6e055e

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ba220b70732966dd6fd77b7fc599159e2133c4916f4676e037e4cb3f64418635
MD5 0a04e5f7b772695c36832123819216de
BLAKE2b-256 72ad96030721399f0d88f4ab9ec2341b6c78c3370a4e6e2c840c185579c3a9de

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: ayafileio-1.1.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 90.3 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ayafileio-1.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e5b7ea7248e907505a41cbf9f17b5bd5a149df111fee235a357b2a106050db73
MD5 600ab84b68101913a1609a8b3450f797
BLAKE2b-256 95ec45bf63e9b0c97ae3c6cd3cb98d8dc99ab11076d23e96919382dc46a35291

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cb54d0f26635f7a1e6960bf7706eb035175f5b07e4c5e6409664d059fbc78c5b
MD5 cd79351b14bbbc62a036d75cb0c9d0a7
BLAKE2b-256 d9b565dd3c2469cfcbdb264e9a992628955653361bf6a7b99019d89292c3f77b

See more details on using hashes here.

File details

Details for the file ayafileio-1.1.1-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-1.1.1-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 a1423f78b052798cb273ec9dd3e6a5fba602d7752d88f1f97cc9c00ef38aceda
MD5 063dfc97ca2db1ad95ca6d22ae09c711
BLAKE2b-256 79e751a3df8ff0245218697ab5ac4dd01834e155f5fc9a6a1f41bfa7bd659d55

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page