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 上支持 io_uring(内核 5.1+)或线程池降级,实现真正无阻塞的文件操作。

📸 核心特性

特性 说明
🍃 零线程开销 无需 run_in_executor,真异步平台无后台线程
📰 内核级完成 IOCP / io_uring 直达内核,无用户态调度延迟
高并发友好 数千并发文件操作,文文团扇一挥间
🎴 标准 API 熟悉的文件接口,支持 async/await
📖 文本二进制支持 文本模式自动编解码
🔧 统一配置系统 运行时动态调整所有参数
🌍 跨平台 Windows / Linux / macOS 皆可翱翔

🛠️ 安装

pip install ayafileio

系统要求:

  • Python 3.10+
  • Windows 7 / Server 2008 R2 或更高版本,或 Linux (内核 5.1+ 可启用 io_uring)
  • 无其他依赖

🚀 快速开始

import asyncio
import ayafileio

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

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

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

asyncio.run(main())

⚙️ 统一配置

ayafileio 提供统一的配置系统,所有参数可在运行时动态调整:

import ayafileio

# 查看当前配置
config = ayafileio.get_config()
print(config)
# {
#     "handle_pool_max_per_key": 64,
#     "handle_pool_max_total": 2048,
#     "io_worker_count": 0,
#     "buffer_pool_max": 512,
#     "buffer_size": 65536,
#     "close_timeout_ms": 4000,
#     "io_uring_queue_depth": 256,
#     "io_uring_sqpoll": False,
#     "enable_debug_log": False
# }

# 修改配置——风势加强!
ayafileio.configure({
    "io_worker_count": 8,
    "buffer_size": 131072,      # 128KB 缓冲区
    "close_timeout_ms": 2000,
})

# 重置为默认值
ayafileio.reset_config()

配置项说明

配置项 默认值 说明
handle_pool_max_per_key 64 每个文件最大缓存句柄数 (Windows)
handle_pool_max_total 2048 全局最大缓存句柄数 (Windows)
io_worker_count 0 I/O 工作线程数,0=自动
buffer_pool_max 512 最大缓存缓冲区数
buffer_size 65536 单个缓冲区大小 (字节)
close_timeout_ms 4000 关闭时等待 pending I/O 的超时 (ms)
io_uring_queue_depth 256 io_uring 队列深度 (Linux)
io_uring_sqpoll False 是否启用 SQPOLL 模式 (Linux)
enable_debug_log False 是否启用调试日志

🔍 后端信息

查看当前使用的后端:

info = ayafileio.get_backend_info()
print(info)
# Windows: {'platform': 'windows', 'backend': 'iocp', 'is_truly_async': True, ...}
# Linux (io_uring): {'platform': 'linux', 'backend': 'io_uring', 'is_truly_async': True, ...}
# Linux (fallback): {'platform': 'linux', 'backend': 'thread_pool', 'is_truly_async': False, ...}
# macOS: {'platform': 'macos', 'backend': 'thread_pool', 'is_truly_async': False, ...}

📚 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 configure(options: dict) -> None: ...      # 统一配置
def get_config() -> dict: ...                   # 获取当前配置
def reset_config() -> None: ...                 # 重置为默认值
def get_backend_info() -> dict: ...             # 获取后端信息

# 向后兼容(推荐使用上面的统一配置)
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 + 机械硬盘实测——即使是即将报废的硬件,文文依然能跑出速度。

🤝 贡献

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

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

📄 许可证

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


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

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

Uploaded CPython 3.14Windows x86-64

ayafileio-0.2.5-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (104.3 kB view details)

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

ayafileio-0.2.5-cp314-cp314-macosx_15_0_arm64.whl (69.8 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

ayafileio-0.2.5-cp313-cp313-win_amd64.whl (81.1 kB view details)

Uploaded CPython 3.13Windows x86-64

ayafileio-0.2.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (104.4 kB view details)

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

ayafileio-0.2.5-cp313-cp313-macosx_15_0_arm64.whl (69.9 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

ayafileio-0.2.5-cp312-cp312-win_amd64.whl (81.2 kB view details)

Uploaded CPython 3.12Windows x86-64

ayafileio-0.2.5-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (104.5 kB view details)

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

ayafileio-0.2.5-cp312-cp312-macosx_15_0_arm64.whl (70.0 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

ayafileio-0.2.5-cp311-cp311-win_amd64.whl (82.1 kB view details)

Uploaded CPython 3.11Windows x86-64

ayafileio-0.2.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (105.5 kB view details)

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

ayafileio-0.2.5-cp311-cp311-macosx_15_0_arm64.whl (70.9 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

ayafileio-0.2.5-cp310-cp310-win_amd64.whl (82.2 kB view details)

Uploaded CPython 3.10Windows x86-64

ayafileio-0.2.5-cp310-cp310-manylinux_2_28_x86_64.whl (105.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

ayafileio-0.2.5-cp310-cp310-macosx_15_0_arm64.whl (71.0 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

  • Download URL: ayafileio-0.2.5-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 83.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.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 47d21caa3ef0a45df2b69f6c86bf10683d9c5bdb7201366cbd21f29eb8142b36
MD5 73b6e5f8285076f470e8719caa37d03d
BLAKE2b-256 0c41c747edec810069d11da4ea815b9cdba8e8b0633ab05cdeb1e2513978ce1c

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.5-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 83e38eab64a69708193c14b95e7873a9aa96e42366e70be3e49a1f2c31e7b6f7
MD5 b1b2ef99193adb705a05a54048f427c8
BLAKE2b-256 3d3da2b92ef435ce5bfa76231b1ee1adfa8bb3d72778050ae073da524d866d8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 9194154c7005f20f3fcf4deecdfa22a0a003c11b19d1ba4b16dcd7cf070a0e14
MD5 4e858491ae400d8e5f9a3136b68196c2
BLAKE2b-256 74c100299b5b06e3f20301e932ded51d9a3625061f1c274bafb25500c35b1633

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.5-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 81.1 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.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c522fd97b6f2af995c2810376f8139dabcfbdaebab9cc380dd59ed810ddbe710
MD5 2c1720765821a4d74cd75c0fbd748ebc
BLAKE2b-256 32b58d394e22ae74d4a67ab0035b6c308dedd6d5582cdc1059e36ca4aa6c3b3d

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 de6fa3e4a1fb0cf1e85046e5d735e1c7d341a1ec2c06f34a0062dc4bf9f4c3a3
MD5 a5cbca51ef05083714d580ed3bf1ecd5
BLAKE2b-256 03e7d0385ea157d96624beb867ffd2e77dabd65f77fd1a5a0c798596db93cded

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 9cf213e2fe9283cc48ad8017451739d95eca03a8c842917abea9509e63e64758
MD5 97b4bd430923c38b8fb0ac5d7652d095
BLAKE2b-256 f2ccb08273266d6483a9c6f1ab06c771c6cb2bcc3e1359752afacbf53ebd7c53

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.5-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 81.2 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.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 efd5979abac54bf5270f030bcf5530e80f08db58c2fbb030bfd40550e478438b
MD5 b7189ae7e5ce6dd91197f4acbd3b9ed1
BLAKE2b-256 2ad7493ff25c5c03942cc113f6e2d6ff69111f9aeac33452eda34def7e55b599

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.5-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fcda61a4626f1e9b79e4930f4d7dd1e1ab5dc312e5f04ab8c25e9ead44d7f2f1
MD5 bd4871d16b761705f995cd71e728a30d
BLAKE2b-256 fc9cf6e33c2fc357aba31511d2e2c62b8b21bb8c24ca435a633cc571af112010

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b94ffd96b52b953f2c715149472486f60f42e3ffd712d1fb01e1d035f55f2ef3
MD5 a62ad451b9d0f022b7835503bb237778
BLAKE2b-256 ab6a644c5c8ff364d429dbbb54f899d7096e37a57bf4215feb7aa2d94e25fccd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 82.1 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.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e1bed10557fdd00157f7b95f1ac37808f384503363006d6d8bc443481513ef91
MD5 c30c38e90e05c6f279cbef184b1f3f92
BLAKE2b-256 5e66b59326fdbcdf3681e55c6054500459f5125131f69abf454ba44bf4aed365

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ac3427c7374748d3838305959141717c606a5e4ef22c649ecb02e9581b0e67f6
MD5 2807c3605e369487b25229b36dc2e0fc
BLAKE2b-256 42c12a35546737bc8fe0f81d92e0e451ed9b3a5ec8f0929276de3b04f89f3b3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 48aa5ae18d3545a1491fd29d474caca554fb794dc9c70eae6aad24a7bcd8c76d
MD5 32231aec2cd340354dc44d80a3c86a59
BLAKE2b-256 f212c17c1adc486856c33428325d9a78809909dae97988be82c807fdd255d8f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.5-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 82.2 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.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c28aaf4e71d17ecfeca002b46070f2d548bf721372baf05ef89d7255d7b101a3
MD5 0fd09a5f239e7544f1dc88dad22baf93
BLAKE2b-256 6dd513fd660aec733e8f4789adfbc2e80de2ec8debe952a4c83cc66c2f9737b4

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.5-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b97caa44c92099df5853a8bb548e50862cf57dd48888390c62c9ca58888db9de
MD5 d9a99e2348954c02da54fde5dbe1b548
BLAKE2b-256 14c01fa6819d524539d820c75a870b91137d9aefeb91b4d8929eb0d58c968b76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.5-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 2bf6ff240f12b348731b4cc236f9719ce9fe23d3fe9886348e390a18dc215663
MD5 d141a5f77f13f4ef41171b50fe22596a
BLAKE2b-256 4a836910e81cafc235cb8af97cf7b0c7e7e1ab7f3e55be8b48ac666baacee0ec

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