Skip to main content

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

Project description

ayafileio

License Python Version Platform Speed

「幻想郷最速のファイルI/O、風神少女の如く」
—— 射命丸文,今日も全力で翔ける

跨平台异步文件 I/O 库,使用原生异步 I/O 机制。
Windows 上借 IOCP(I/O 完成端口)之力,Linux 上以线程池模拟异步,实现真正无阻塞的文件操作。

📸 核心特性

特性 说明
🍃 零线程开销 无需 run_in_executor,无后台线程 (Windows)
📰 内核级完成 无用户态调度延迟,直达内核
高并发友好 数千并发文件操作,文文团扇一挥间
🎴 标准 API 熟悉的文件接口,支持 async/await
📖 文本二进制支持 文本模式自动编解码
🔧 可配置句柄池 根据工作负载调整性能 (Windows)
🌍 跨平台 Windows / Linux 皆可翱翔

🛠️ 安装

pip install ayafileio

系统要求:

  • Python 3.10+(幻想郷的科技也要与时俱进)
  • Windows 7 / Server 2008 R2 或更高版本,或 Linux
  • 无其他依赖(自带团扇,无需外援)

🚀 快速开始

import asyncio
import ayafileio

async def main():
    # 写入文件——像风一样快
    async with ayafileio.open("example.txt", "w") as f:
        await f.write("Hello, async world!\n")
        await f.flush()

    # 读取并自动解码——文文新闻,一触即达
    async with ayafileio.open("example.txt", "r", encoding="utf-8") as f:
        content = await f.read()
        print(content)  # "Hello, async world!\n"

    # 二进制操作——数据如风,来去无痕
    async with ayafileio.open("data.bin", "rb") as f:
        data = await f.read(1024)
        await f.seek(0, 0)  # 风神少女,回首即原点

asyncio.run(main())

⚙️ 高级配置

句柄池调优(Windows)

高并发工作负载时,调整句柄池大小,让文件句柄如天狗之羽,收放自如:

import ayafileio

# 查看当前限制
max_per_key, max_total = ayafileio.get_handle_pool_limits()
print(f"当前: 每键 {max_per_key},总计 {max_total}")

# 增加以提升多文件性能——风势加强!
ayafileio.set_handle_pool_limits(128, 4096)

这会在打开/关闭周期中重用文件句柄,减少昂贵的 CreateFile 调用——毕竟,天狗从不做无谓的起落。

I/O 工作线程数(跨平台)

# 设置 I/O worker 数量(Windows 上为 IOCP 线程数)
ayafileio.set_io_worker_count(8)  # 0 = 自动,1-128 = 手动
# Linux 上调用此方法也不会报错——优雅降级,是风神的风格

📚 API 参考

AsyncFile 类

class AsyncFile:
    def __init__(self, path: str | Path, mode: str = "rb", encoding: str | None = None): ...
    async def read(self, size: int = -1) -> str | bytes: ...
    async def write(self, data: str | bytes) -> int: ...
    async def seek(self, offset: int, whence: int = 0) -> int: ...
    async def flush(self) -> None: ...
    async def close(self) -> None: ...
    async def readline(self) -> str | bytes: ...
    def __aiter__(self) -> AsyncFile: ...
    async def __anext__(self) -> str | bytes: ...

支持的模式

模式 说明
"r", "rb" 读取(文本/二进制)
"w", "wb" 写入(文本/二进制)
"a", "ab" 追加(文本/二进制)
"x", "xb" 独占创建(文本/二进制)
加上 "+" 读写组合,风神双持

函数

def set_handle_pool_limits(max_per_key: int, max_total: int) -> None: ...
def get_handle_pool_limits() -> tuple[int, int]: ...
def set_io_worker_count(count: int = 0) -> None: ...  # 跨平台通用

🧪 性能对比

在同等环境下(测试时长 10 秒,随机读写混合),ayafileio 展现出了天狗般的速度:

并发数 ayafileio aiofiles 优势
50 2,043 ops/s 1,657 ops/s +23%
100 770 ops/s 405 ops/s +90%
500 1,130 ops/s 1,032 ops/s +9.5%

P99 延迟对比(越低越好):

  • 100 并发:ayafileio 33ms vs aiofiles 562ms
  • 200 并发:ayafileio 35ms vs aiofiles 155ms

数据基于 Windows 10 + 机械硬盘(C5=12, C6=45)实测——即使是即将报废的硬件,文文依然能跑出速度。

运行基准测试:

git clone https://github.com/your-repo/ayafileio.git
cd ayafileio
python run_benchmark.py

🤝 贡献

欢迎投稿「文文。新闻」!请:

  1. Fork 本仓库
  2. 创建功能分支(git checkout -b feature/amazing-feature
  3. 添加测试(天狗也要认真对待)
  4. 确保基准测试仍通过
  5. 提交拉取请求

📄 许可证

MIT 许可证 —— 最速最自由,详见 LICENSE

🙏 致谢

  • 感谢 Windows IOCP 提供的真异步之力
  • 感谢射命丸文带来的「最速」精神
  • 感谢所有在坏盘上测试的勇者们

「遅いのは罪だぜ?」
—— 射命丸文,『文文。新闻』主编

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-0.2.1-cp314-cp314-win_amd64.whl (75.5 kB view details)

Uploaded CPython 3.14Windows x86-64

ayafileio-0.2.1-cp314-cp314-musllinux_1_2_x86_64.whl (536.5 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

ayafileio-0.2.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (90.0 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

ayafileio-0.2.1-cp314-cp314-macosx_15_0_arm64.whl (56.1 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

ayafileio-0.2.1-cp313-cp313-win_amd64.whl (73.3 kB view details)

Uploaded CPython 3.13Windows x86-64

ayafileio-0.2.1-cp313-cp313-musllinux_1_2_x86_64.whl (536.6 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

ayafileio-0.2.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (90.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

ayafileio-0.2.1-cp313-cp313-macosx_15_0_arm64.whl (56.1 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

ayafileio-0.2.1-cp312-cp312-win_amd64.whl (73.4 kB view details)

Uploaded CPython 3.12Windows x86-64

ayafileio-0.2.1-cp312-cp312-musllinux_1_2_x86_64.whl (536.6 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

ayafileio-0.2.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (90.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ayafileio-0.2.1-cp312-cp312-macosx_15_0_arm64.whl (56.1 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

ayafileio-0.2.1-cp311-cp311-win_amd64.whl (74.4 kB view details)

Uploaded CPython 3.11Windows x86-64

ayafileio-0.2.1-cp311-cp311-musllinux_1_2_x86_64.whl (537.6 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

ayafileio-0.2.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (91.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

ayafileio-0.2.1-cp311-cp311-macosx_15_0_arm64.whl (57.2 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

ayafileio-0.2.1-cp310-cp310-win_amd64.whl (74.5 kB view details)

Uploaded CPython 3.10Windows x86-64

ayafileio-0.2.1-cp310-cp310-musllinux_1_2_x86_64.whl (537.6 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

ayafileio-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

ayafileio-0.2.1-cp310-cp310-macosx_15_0_arm64.whl (57.3 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

  • Download URL: ayafileio-0.2.1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 75.5 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-0.2.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 99414b56f482fdd4ca8ee6bb471580fb2e86a469ec8f40c672a81f9491cc1922
MD5 8351ab33d1810c6f86561e8fc5009393
BLAKE2b-256 178b662bec02d551e094db48bd6104c6948746e61f8b0b83b9086223de39a7de

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2586e3493cf06bad58111b306de5cce64ea763640549e74e923a7c54feb0eff3
MD5 14e22b55f0fe7f393cc6b38664723efc
BLAKE2b-256 8edff08675fd2abaa6e933314ff9ac91ee4d69c0ad17d738ae0d6e080c83a6e7

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 be952d87fc4f34430f24ea4f50c67fb16baa6c06fedde2ed8af90a9df4f8a2ba
MD5 a2cdde27e3b2f252f84025f47a5ec6ce
BLAKE2b-256 082c421c6ed1a743c7112fa394bdf2254cf610e28fe75f17ba82dfa5640d5889

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 92a6764f333ecf7aaab4c4dcc287336877a3b82d6171daeea144ba5669394734
MD5 980bef1c9cf784a65d0e93b7a205442a
BLAKE2b-256 27b9c6bad1dffddbb6aebf57bf072f6a0808746d9200483416b6f0655c1b7f9d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 73.3 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-0.2.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d895eed8f0876334cb32a756d7dc383ac913687c53e7975a15f5dc54960ae21a
MD5 46c717e44b89b2dffd887edc5155d6fa
BLAKE2b-256 5f69f5c6eb908b23c9261b34831648d5d5f9fa58e82ea77a3cbb92b8911e77eb

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2b160efb6fec52122d6938c21d0277083b89b33db68e26f9f174d91290365521
MD5 d5dae7b318c37de35a7328e46795edd7
BLAKE2b-256 c737271d2295f11adae94a7a360719a0d15d3311297d4eaa87d34117eba9c218

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e08c671398b9e7dc63ccde8dd76a10264563444e261b1dc26b613f962c4b557c
MD5 5819f8abe580ac953448a77edcb6b017
BLAKE2b-256 e2001c8725c95027c50c47a0c659d31513e53ccbf72dc69a0098e7db96d52830

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 99615251c85a4f560fdabb7ea7089f1a55eab55e2cf613b9899b025bb7b96e64
MD5 1f2026230eeca965708193d61a42760f
BLAKE2b-256 700f8980ab123c5480bf74e15ecdb5e2303f94cf12ef365e8acd6126560cdd49

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 73.4 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-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ca395a87032f1b298d4e1528cf4a9340ab5be6e76ef6f45f8f4a19f95796b0ca
MD5 cd9392a1c75dad31939d7deba24f4a04
BLAKE2b-256 7ebba82731d801a27b0b27a6d9f866a8d3be6d02e784eba363a2feb43459fd13

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ef1637c1835d21d61e7acc7c27e0de3cd04cb949c676afcd52f437fb8952ed07
MD5 131c66a4e918bc51ce7cfbd1697de9a6
BLAKE2b-256 477c3be122bd7bf91f518b6d04cf58d846ae340ee065f93eb53cf29ba0fdd5b9

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 754631ca443f57963465cad24c673270f197c86b02d1d29193bf479a02a58f1c
MD5 e35dfd878fbaedbcc908d71a4c41aaca
BLAKE2b-256 3d9ad8c5b956fe1e56a852cca66b5d894c543006fa569dbc3865f821bb1e2bb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 58261e4bc87367c00138d852aeeaf0b6c9db4e22a858ab17964958baaced4562
MD5 f66a3c9eb510a75a748a9208ab0b34dd
BLAKE2b-256 6742830a30ac72cfd7b9ae9a07ccc57946e87fd853c70810af5ff4c022812f72

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 74.4 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-0.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 30bf4e81da849b0f9fc7bfb9bfc7c452cf631a6efcb10465343cd70d4c6b4b30
MD5 0766b2f97bd921d064d898ec88587a8b
BLAKE2b-256 a6f492cc18fde7c6138561b5eab0889d2c92349ce2911da453144366f8a106e1

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dc382019ea49cb602e4b9b50e915cd27aa5538174e75f49e116dd8698200616e
MD5 132a7f5de1acb797d50ea77ab38e0c42
BLAKE2b-256 80530658a7b7c6a37d1694c46d6fbed744054ac396376168bff26d1ab7793f3f

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 00f609e15f88f5e517bd012e0978e0dbf4fe8553acbb32e2ba1954bbe9cd290f
MD5 ea76f05593912e86200922a1a01e010a
BLAKE2b-256 00ce57e21a9909671a77789406eb4ea5d668622914fc4ae90fcabd95ccf501b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 aa0091e1aeb7ffafea7e41210785fa294a4955db85d4d7eb4eb6d6858872331a
MD5 f9bb7f2584b8cc11028ef74254d6373d
BLAKE2b-256 dbb3e1aa4a1a99c3448d8a916a7a9a9bd66ee03c1227f8ae9792edb34f758a01

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 74.5 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-0.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e67c447720ee8db4264ae82412c45444760a10719aed15955777c41954c63784
MD5 4daa2710e624fc02ab2b1f1b355310df
BLAKE2b-256 6b502d0e72ec89a990aae538f83c7292f1462aea35ad01fc127891b7ba312cd8

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a589fcae246c3bd3c04982f262b368cb8a641b77010c65f15c6f6c4a1f6acfa2
MD5 bbd3a4bb5a36666256707e27a2034070
BLAKE2b-256 9ca16eec6c13fdf9d75aa9a45723f48b873e0084158fd9c13b7e7415a711db05

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65597687e55d806267f758a3c78d4381c8febd5481b765349463be0de631d670
MD5 1f756d5b3ab115ac8d31e26bf8b2649d
BLAKE2b-256 4a9412d8fc44f9191da063da915ee33b0bf5cadc80b39d177d9c6b614d1adb5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 85b61d8d659ee164edd727948f3e43529fff500f8a3f0a31c065173066f2d7ee
MD5 e0a68ecf83ea79474157e831c2d8d20f
BLAKE2b-256 a2891a67da579665f607fc60d6146e29b6c355c1f98a5953848487d5051e710a

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