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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

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

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

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

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

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

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.12macOS 15.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

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

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.11macOS 15.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

ayafileio-0.2.1.post1-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.post1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 8ba9a5b7cf7e83c75d9b5ef125b797323cc8cc374b4d7386e5b1448111f9c8f0
MD5 60e938fc525525bc4a47de752f285d3e
BLAKE2b-256 28190f7909565fea53c48b6c9c5a566fa050bdfa38ebbb939c0a99cd842ab143

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 50ba28d63c2061fab5c6692c93f754aeeb82d27a8f93f077e5612bb31a05f9c8
MD5 d6f5e4af6437edd9d88b54972b56bc13
BLAKE2b-256 9e1890187209f3f4af95f2c439f82d49fed8a1e98b18d8379e33cf7b8b918ed8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8af1e045b450e5be83f86311dd17e2ff91ede782587423fbe7b0535c363f268d
MD5 17b8c5aca0b4703c2d0eafbd2e91a06d
BLAKE2b-256 b5d78566a242f4525376c734d433b0e48fd89f0e460ddbbc17d32c66fb669462

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 568f218f3db19038d548903ca49560f99357efed332842b41999cd6e8cbca39d
MD5 0b2e8960b536d17bfc6b851b69fd4f35
BLAKE2b-256 15f05b00f94b78bd3b33a4675cb2806d5d1488bc8dab32245df767dabee05e08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8012608e174221fbde6f46b8bba562ccaaf90e6abbdb29ab3bd2cb8ca31815bd
MD5 d024b179bd88a881957f663aec9292e0
BLAKE2b-256 5f75f90eee58f3f9785571ccde1d729c1fdd149e3e6cdf522b09c5c67bfb4107

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a9593935d4b2999379721669671f5ee699023bd15f3bc261d954fe134badb3c0
MD5 9d500a4fae9381782078ec85d94c2552
BLAKE2b-256 fbcc60d93da74453704cefe0f4eb6eff739efe656bde5f5cd6202d11c49d799f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 30ee229e9b18d6997050601fe3afbd84a95e242df8099c8c8b837982608bcf09
MD5 9cc857c718ff9b7885bad71ce6db657a
BLAKE2b-256 95d861a43d0d430caca62a3b1fdadc2dc7b1b77e089f2871e77a90ece9020366

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 91c57a16fcd26091ef9aa3b46ca804934dde1df1daac2a2afd80bd341af196a6
MD5 448ad1aa945c7366c009f24710df025d
BLAKE2b-256 ba2450ebe1aa66f66fb3f78c1bc224fe441a1c862a035415a83da08ea1dd6c6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 77518662cd00545e82ce35261edb32efbe6f0a70b836f3dd44e1ff79840cfbfb
MD5 bddae819268af2c8ef7ac1abf5a89e99
BLAKE2b-256 036faed3c3e942ed7ba0941733c4e96a2c50b9b66a5bf6e4c3945c5997ee3293

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fec3d9aab3c43bd55fec21e76e26e42d004b0930a49ac2956b22cf1339b3f19b
MD5 8e998860e4e84bf1f9fd2020abc8d2cf
BLAKE2b-256 dc7092074d0e0b67d8ec5c0659cdd645e0418c180b8d1dbf8aa2025034530c49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 062a1cc7faaf67f81af03b5c592eb4f3249291afb147167e51ba26505056ffbd
MD5 ea3f3d0b97c60d8f7011ce2486c4c815
BLAKE2b-256 db0199583e352e0885481f1cb0652df4d70e72a246fda419b48bc241ee19e10f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 784cb3fb45e738b5f5a06512151117992ecfaffc21a688bb0761df7b8a8f83ba
MD5 4d17696fd04b557d1cae39c175babfdf
BLAKE2b-256 fdec143c9fd93af00a60ae4c8a3e8714a0a2f4c947e8a8f14b018509bc6415df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ef9bfcc55026a6837f18af2ea251edc01be5e71deb124c57c75a544715ac4705
MD5 cbe59158ad18a1850fdc6fcc97131047
BLAKE2b-256 696301cc4f1c6a58d83c2a7dd9351689cc9882c2f8c51c9d19bcc2c62db8ca47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4635646580b61dfdfb0c168e50432ea8d1e3e1ffe6dfc65c434bb36f3c2c5c66
MD5 bb22327e699ffc5797f9807fe2a361cd
BLAKE2b-256 a076b58c79570bfe07cb4cc69ac54e57ce983fa6844924ae99d9a7ba004865aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 c4374e8cfe010bedf995cc99aba3b8dba2a942b6cc58b9824431fa47a40912d8
MD5 b9b2891b226950fa08d92de896e7cd47
BLAKE2b-256 9d2e50a2d75d220f8750282c15b9c0abdfc05c518ef79c7097fb623199437742

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 5260d4e4ba61937b591884defcb2f027d54a1e5699f0a2b7a4b042bf37aa8e64
MD5 5d1d39d32c57baef3d481457b92bfc6e
BLAKE2b-256 4dc37235f38ebe59d068e1c01dfc7e7a9518d9ccbdc5e2bc5f85283df2854f2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 942d1195e8414960908bec0289528453f6fda5bf29b80e53c46b7d66e6617834
MD5 301ab3edeeba147aba48feedac74bf35
BLAKE2b-256 72e74b828217f1fa4d9d613cebfa848b84d0f3ea4cd6b94c77bb0d080979a75f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a327060df2a3913d84f639ff25dc907b453db39dbbd4d402d06c73fa8e670ef9
MD5 3c805a64a93fd0de1cf87ab7e0460a6a
BLAKE2b-256 8c865c3abd354f21bed36657beaf60c67cd71bf76f2f223c21ebbdb721aa4ef1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1ecea808ac0450a581655fe2a1c154cbd452cf3b0bf77fc9cc9fc0b5d0f868ae
MD5 43dc726cb7ff9a5418bff49b875a46b5
BLAKE2b-256 c5c965c6e7a02b000a3b026f8b2794242129579d88762335275fe4941e566b6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post1-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 e3281d2c72bfee5c034885771719be613b76119a2202fb7477a85c6fa89b4d97
MD5 b3b94f618d3a048a71bdaab974995f24
BLAKE2b-256 0fe699d37a149e4bb0493c2afee061f10ee3e8d46a68f7b9fb9d83c9a8fafd05

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