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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

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

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

ayafileio-0.2.1.post2-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.post2-cp313-cp313-macosx_15_0_arm64.whl (56.2 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

ayafileio-0.2.1.post2-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.post2-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.post2-cp312-cp312-macosx_15_0_arm64.whl (56.2 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

ayafileio-0.2.1.post2-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.post2-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.post2-cp311-cp311-macosx_15_0_arm64.whl (57.3 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 e7fad9d631dc5e8d0a85d49b0beb66c135219e93d788f6884160c317380100b5
MD5 0e7e7de7d37beace654b740ddbaf5208
BLAKE2b-256 09ffb06fe9fd0fc23516ec1ebf6fbca6cf4b16a885cfa4ad1cce7bf2583398eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 540cc1e9975f15c410119f32030b09009d0b7df47648fa0b0a0ca8cb3832d7d5
MD5 3c696716cd87db94b984e79c3f4661d0
BLAKE2b-256 7b9f181ccb70932d1a81dd8ee746d0c0a27167d70e4e45ba258b28bb531595d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 7b8ea825cbd04e82787728887db334ea2486f3844fad11431651de3408fee960
MD5 b6b1e6f6d4932a2a6baa1f5406473fd7
BLAKE2b-256 8425ae1f35cc2f1ea1d6b76ff3357279d889b7a41f73085052d567481fff8f9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 1146b5740cba89760e28b679c7de87d872e39d4264ba405cda7e87484db34c90
MD5 cf048772f15714d0e428e1cec5790085
BLAKE2b-256 af796ee5d0d1eb8364ed624ba7bbf578e1248abca75b34f7d963e42403aa140f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 5adbeb2b6c726dd2a7e78f0be886057cc736b9791d6c948665faab293e3793a2
MD5 a6ddf3105a2aa74fbb14b5270ca1c637
BLAKE2b-256 2355b439897fcf2f2f4f837d3640bd613f1aa8399d82caa65a5672258ce7791e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4c8ea03b24fa10a2af30627a3106120eab6f02bc9b7ec07d59559abf1c1e4f6b
MD5 b6503544f5990f3c64de87d19b2c05f4
BLAKE2b-256 15cf779d683b0221ca1701b7f9a3fdbc268f7f1d5d76e66732ea7215561e9d45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3caf7d2a24832404406b1b4e4fcec46aa778211451c2d7e5bd24f3963785fcd0
MD5 efd3f6f3ecee0ee1ddc81afea85e4b21
BLAKE2b-256 d044d6dd7c2f1934189613c42f816f85586cd0793a730cbb75d7c110a41fdcca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 73677d3a1785a11dc665dbe2dc0f6f3ef94b2727368556394f948b1ebaef998c
MD5 9b49b6202bab8c493cb1a4c69e7c0799
BLAKE2b-256 9310c2c514f87596f04b26e922656a4083045577b39fda10934d904e7af26769

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a056b80180347c0690fa908a46cf79a129f57bce8d5f742636396cfb12aecd2b
MD5 9abee7f7c321d2434118017b0ad8aae0
BLAKE2b-256 fc075dd9b979091e1f64b23107952fdcfd639b014df1ec2889e2a4475e7ed97b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 af9055b8a8c2d0c9d6346ce9aa5444a111ed653cfdd15172c4605161b468fc3a
MD5 fd6692cace0d838f5fec4be8cced1659
BLAKE2b-256 e093e583b771b46e0031359fa0f49b67d4336f016984b1f71245802fc680a232

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 5799c85c60a892c1bc38ef4ec5970dab22f6deb61e94382d1c23fe23b0e091c5
MD5 e5af6b091fcf21a421d31d7ef64196e5
BLAKE2b-256 aba2ae071c250c88e50978e0048695c2a060c6822f3343ebae5e0e2dc422810e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 fe22ecb2a09515164ed08b48c017887fa5fd2c1c8d0f9fcf63012491a84bf62b
MD5 5adab4279a61206e28b6abdaf0e0bcf7
BLAKE2b-256 0fe21d4e675e6c40e9149669f0028c309b1657233bf5a2a9ee09e932bb057715

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 88362dedd711e46bec2cb7f6f7ec01ff7106318a1ba2ef5e5b98d0887e3c9ea6
MD5 f714a5dce0dfd2ddf6dde25d14835fb1
BLAKE2b-256 61d828eefb7ec4a56c9b4132fc4af515b2c37d665e9d6c8d047e52c0d8714e74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b12078cf2872e4312fa448a73071d9831f08d6deded7d0ffcd95e2ec06cdab7d
MD5 054e987cdcd4cb9ff8fc6bb695cf3885
BLAKE2b-256 00f850f7cc257899af54470dbe383f417a29e7017b7a653fac81d50d849c476c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 0ebbc2d2bf3d2dbb5b0fe076408728a24f72ce2977f2dfce362d975552381339
MD5 17610e97c534af0d3341f11ed72c0a53
BLAKE2b-256 a5ea6b92d51945b4238ddbff3f16bc255624c23fd5fcc44a4dc2d1b9af0378ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 4969c76bd824920ee675b74f65bda1c0a10043e19fb6e59a36407b9bf8756f11
MD5 d1231ebd816210954d420e5688c6e016
BLAKE2b-256 dfa09a45eecdf71a6b857ba1d46bd787777a621b456656946f39c999a60a1ec0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 30c0ccdd8bd433ce9516f3bc11ab79e5903cb483af88ff47b502e39010c01b65
MD5 c119fbc42f6a2496f6ec64a846c0d3f1
BLAKE2b-256 f27d62ca00e56a54c59a3693902a7189121bd8102b22f1c8ddbe83b74af7cf42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c804755c0c836ce7788c0ee3c9da38e5fda17511770b699176ae05c9dfe5afa1
MD5 d3d939c2e529dd87c54b4521d3d41cf9
BLAKE2b-256 e1ab91b2fc3458345fb935b17566b0d2dde28810efef5c08ae7af31841790f04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b7bbfb116086360252be4fe735ebec77f86acb180726c829124f2c1c391fd44e
MD5 7d5c0f47595a9fca779178d8852d5f3a
BLAKE2b-256 f8ecfc54547e4d505afbbb686e123bd767f2897483fcd5f5ae5e50a8699f5964

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.1.post2-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b5b37921e64708038c08f8a4fdc21178e946a179cd84e8e10394e9ae6e9b060d
MD5 f2bbbaf38a574534cb2ce1dadf228829
BLAKE2b-256 26e313dffb9292f7e65c212c3b31cf34cb4489be2bf2c40d5648001aae5743e6

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