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.1.7-cp310-abi3-win_amd64.whl (70.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

ayafileio-0.1.7-cp310-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (76.6 kB view details)

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

ayafileio-0.1.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (88.7 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

ayafileio-0.1.7-cp310-abi3-macosx_15_0_arm64.whl (54.8 kB view details)

Uploaded CPython 3.10+macOS 15.0+ ARM64

ayafileio-0.1.7-cp310-abi3-macosx_11_0_arm64.whl (59.3 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file ayafileio-0.1.7-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: ayafileio-0.1.7-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 70.9 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.1.7-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 30894f1605efed48cef1f4b1bf9eb1c22d6fc969d3c0181676f6511a8fe982f9
MD5 074fad9b91a65c40b1545b8e2f5e455e
BLAKE2b-256 b1e38936d9fc84104b2dcfff4d68ce3fce2e0f6b25d48105d79c0c6982abdb1e

See more details on using hashes here.

File details

Details for the file ayafileio-0.1.7-cp310-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.1.7-cp310-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6b219940c5407c2064bc6c01eb3ae1ab80b9ace9c8981d8944167f2f5dc6bbb5
MD5 00a63586e88adcd26e6436ad40a3ac9a
BLAKE2b-256 9709bda5845e15a5ae60b92aa94fe935ce600d0c553c5ce8b8a7250f1b0791f1

See more details on using hashes here.

File details

Details for the file ayafileio-0.1.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.1.7-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dc33a6cfd77476017982f903d5bce5fb67a30b621448c3792ca4c25a00f76793
MD5 5a1af1649479a9e63622c9c274202da5
BLAKE2b-256 e4cf8e7770b7cb336427567091c5dfa4f97cf692ee789c145882dc391ab25721

See more details on using hashes here.

File details

Details for the file ayafileio-0.1.7-cp310-abi3-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-0.1.7-cp310-abi3-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 2096ed241b650edf052d6faf6fc8752707d26e9565e3f1482b0c96f2bb594904
MD5 dcba93bbe6edea85ddd761fe436550f3
BLAKE2b-256 bb824e267a720dbd6108aa3abd7fd4e43d676d11948ec5dea5c50e5126118b46

See more details on using hashes here.

File details

Details for the file ayafileio-0.1.7-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-0.1.7-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 125733b2cd450ce904e0c241f51ae286d15569b0dae8d5d47d931ff4651bbf19
MD5 f1fd9b0ad26edaadf6a3713e5bce6f16
BLAKE2b-256 d753f1b49284fe4c4a5ed25995562812771fd53741279d39c744d81c5a4fdf46

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