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

Uploaded CPython 3.14Windows x86-64

ayafileio-0.2.0-cp314-cp314-musllinux_1_2_x86_64.whl (536.0 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

ayafileio-0.2.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (89.6 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

ayafileio-0.2.0-cp314-cp314-macosx_15_0_arm64.whl (55.6 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

ayafileio-0.2.0-cp313-cp313-win_amd64.whl (72.9 kB view details)

Uploaded CPython 3.13Windows x86-64

ayafileio-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl (536.2 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

ayafileio-0.2.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (89.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

ayafileio-0.2.0-cp313-cp313-macosx_15_0_arm64.whl (55.6 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

ayafileio-0.2.0-cp312-cp312-win_amd64.whl (73.0 kB view details)

Uploaded CPython 3.12Windows x86-64

ayafileio-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl (536.1 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

ayafileio-0.2.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (89.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ayafileio-0.2.0-cp312-cp312-macosx_15_0_arm64.whl (55.6 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

ayafileio-0.2.0-cp311-cp311-win_amd64.whl (73.9 kB view details)

Uploaded CPython 3.11Windows x86-64

ayafileio-0.2.0-cp311-cp311-musllinux_1_2_x86_64.whl (537.1 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

ayafileio-0.2.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (90.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

ayafileio-0.2.0-cp311-cp311-macosx_15_0_arm64.whl (56.7 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

ayafileio-0.2.0-cp310-cp310-win_amd64.whl (74.0 kB view details)

Uploaded CPython 3.10Windows x86-64

ayafileio-0.2.0-cp310-cp310-musllinux_1_2_x86_64.whl (537.1 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

ayafileio-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (91.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

ayafileio-0.2.0-cp310-cp310-macosx_15_0_arm64.whl (56.9 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

  • Download URL: ayafileio-0.2.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 75.0 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.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 619b47be78299f749607440758c4a4b21404a10c313cb33c42dea5436a20bb8c
MD5 7ad33ddac77b447871f8d38f3f44b161
BLAKE2b-256 a3d14d3e10eb0a133591a8f732f6c6184d09264b866c8cbbbd38a72b7038d487

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 95d325bb02fc36d158bc4f3df7b47cc30405db79cde57edcd1c106f4fdb3272b
MD5 9559df1b557b46ccbca9ec522ccc3b61
BLAKE2b-256 c29754363e367f01e5799ada922a57ab1d51dde85e35f4f8712c0cd9b932e313

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8fd78ab5238e6b7c23cf19a848a7dc71a46f7ccf91b4d0553167a6f5181daf61
MD5 e24e4be68cb2edd153fdeed8dea958f6
BLAKE2b-256 357cc9a24c7c83111df459113ad78aad3aa2f1e2f290c997bc624bc36ec6e852

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 cdf63d42a4b970a3f61d5c85b716f0aa5b58b8752c7077c0d691652ac049fb90
MD5 4d3c913b0002181ffe03eee08f9f2cd2
BLAKE2b-256 35220a2c0c00314839a71f7a1c481e25249529779b129c9d626180057f7d39a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 72.9 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.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ac7e43962048ecf3e1a46f68848ff9b7c6f8d9f5d63391b496b765b7fe4e9efb
MD5 a27b9a025dfb75a27e6b8a7ebf52cae1
BLAKE2b-256 fe3c24608c01763ef6898ccebb0cb9fad8532c1b0c8afc0908b7dd497c284182

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3886e7bb3fc6f8abfeab5b98fd575548a1036f1d5f8040fa9e17f23ff9333ef8
MD5 61e472ae29701ae29e4e492d886c05dc
BLAKE2b-256 d7cc6e27c07d3b9027770cd0f40055180d70a95763a90aff427f4b103ee4779d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 938e634dca790f693f6c7f5a4ca3b140198ebe316b02b11cc31c89c62c8b7232
MD5 cd5c67a64596dc2f489ebcaf4d4c0874
BLAKE2b-256 efa58269c73015e3bc2a12822c70600002f4f688df625227e2ab73acc09fc024

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f85530b9bf62f2061b6c46ea976b7d9ebc164b4e6e18ac901b5d9cf9b319cf07
MD5 f4ed9939cd965dd36c212761af8e4614
BLAKE2b-256 7408f0192d86049a6b7530f3de7e22f848acaa84c3fce48e88cdc9fdfcdd0920

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 73.0 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.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6e31aacfe567749bdb8d92efb079c29626c241c8650d8fd4250f868198478467
MD5 349af488bbabd7182a1b4e7606cef076
BLAKE2b-256 0f21c6e74f6aa79c23e9402092ca6f3cda5281591466e506fe64e3a4d5fca1dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7d570c196cec90b8ba162326f40c408b058c0cf9cbb393946f3eab5a7b8cf9c8
MD5 897c743988b606484eb12820d98ff949
BLAKE2b-256 aeb9d231f2a98c60a9509f440b04a0aa8bf9495ac40270b078aeca086f427aaf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 f56c14f53706a68dfa5fade862b31cd1b79c4b1d214c9b2486d5820582ac8238
MD5 1bbeb26a22c910f81d45ac56afcb1c8a
BLAKE2b-256 ce5d51f859871a658e8a5de2fd63c784257186269d23f5c994261235987fa9ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 4ff3bec5f351c4dfa799cb752cb6ee624208d8f34c7eee03f5ee2f62f7d38721
MD5 82aa36384997f85f73366837fa45a3a1
BLAKE2b-256 f2715920bf912ad71e1e7da245364d5d9b77f753f17ca5457d23de83911a3272

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 73.9 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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ce0ab9905c09baeb1d5c80b30eabd622044f1afa4bf86c052bae09af735b046d
MD5 601bb632bf478c0af414a1c6261c812d
BLAKE2b-256 0da2df7f931f8a7e946ecbb0db64442f18100a5c4fdb8f3741969b0b628a2bbc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a847fa45d18e7c6aefa9cd5d65fadde6973d27a6b531122c8ff4de485b5c95e1
MD5 89ad08b90c02a11c2b89c4bafdeddcfe
BLAKE2b-256 cfe1e6f8ef616bad5e00d0d1c73a8ef491f40290f3e84a883210e7a4d97e8f4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 8e08b6a676ff10af7f06ab89ffca22837b961ddd8460d38a40056940dc3f8737
MD5 4b0e4162cddb2ec94bc57dbb6a833417
BLAKE2b-256 36e5c69649e36ad0bff43125101319ba86935dd9d1d0f5b9e392b792e57b8523

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 45d0cd36e8cb61b8f4e9ca28347b7538951022d98c73a1c2f62b9f9019960dda
MD5 0a40b5a5441e26316cf39afda46c5815
BLAKE2b-256 7ed016833d2807d7ddc0f84d8adc56e70e9cfb2ee3c340baf9e597b4dac4ea8f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 74.0 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.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 03bc7ac523b6e9457cc5e59932a7d1d5b0c6d672765ddb75ed1027623384f777
MD5 605079467f45d5148b23daaa2192d88e
BLAKE2b-256 1ac95147a9a14404cdfed5bfc70b3b799cd5e4800dc31c50fe16ea2c13ab3a92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2e759132cce64520ec871ecfb0484a4be3cbd6fbbd8ac8fc6a01041b48a4c13b
MD5 6e6ad76dafb78240de247533add0076f
BLAKE2b-256 893df9f76b09ad422981c6e7e299c5f8daa9d92f2cb83696942ea4702f379049

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9eb0308440989401d05c6ce563e5ed4edfdd7d30b7d3b8e54b0012b7451d16c8
MD5 4b9c14d66403f477b53f19af05241186
BLAKE2b-256 58d5a48345390ae9cea9dcb39eba4295d05aac1ae4c52771b37b89d82a8495e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.0-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d7cfd4895def190eb7e275ef45193287bf38803a808cfee5d6623eb4471fb609
MD5 901fee8c017d48aa8bafb97da0d44e20
BLAKE2b-256 1b1a7206327dcafbe87d64bc9594be64c81a0c96f6abb12a66495bd44fff6767

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