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

Uploaded CPython 3.14Windows x86-64

ayafileio-0.2.3-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (92.5 kB view details)

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

ayafileio-0.2.3-cp314-cp314-macosx_15_0_arm64.whl (61.3 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

ayafileio-0.2.3-cp313-cp313-win_amd64.whl (78.8 kB view details)

Uploaded CPython 3.13Windows x86-64

ayafileio-0.2.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (92.6 kB view details)

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

ayafileio-0.2.3-cp313-cp313-macosx_15_0_arm64.whl (61.4 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

ayafileio-0.2.3-cp312-cp312-win_amd64.whl (78.8 kB view details)

Uploaded CPython 3.12Windows x86-64

ayafileio-0.2.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (92.6 kB view details)

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

ayafileio-0.2.3-cp312-cp312-macosx_15_0_arm64.whl (61.4 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

ayafileio-0.2.3-cp311-cp311-win_amd64.whl (79.8 kB view details)

Uploaded CPython 3.11Windows x86-64

ayafileio-0.2.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (93.8 kB view details)

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

ayafileio-0.2.3-cp311-cp311-macosx_15_0_arm64.whl (62.5 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

ayafileio-0.2.3-cp310-cp310-win_amd64.whl (80.0 kB view details)

Uploaded CPython 3.10Windows x86-64

ayafileio-0.2.3-cp310-cp310-manylinux_2_28_x86_64.whl (93.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

ayafileio-0.2.3-cp310-cp310-macosx_15_0_arm64.whl (62.6 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

  • Download URL: ayafileio-0.2.3-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 81.1 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.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 51cf5d615f26a509798cc95dbad0bea7b5db3596104c63fa8c4a77442f5a4d3c
MD5 cf06de45b06ddea4d0c86987805e4868
BLAKE2b-256 b53240fa3437d46a5330297696ef3398f5780b52fb2c578c4bfade696cb9ce4e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 38a1aaedc1f0dcd3d81c1505f1877ecc6d4849b8a5cec829e48e8939943ea64d
MD5 2dc6290e667ef9a6db7db605df3206f5
BLAKE2b-256 93adce5c916630b2a9f1419ef90b1232fccb3accd517d8727e3892e6a65e2d3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 1bbafc893f22ac24386ef8f2dfffb0a58a9e85152799083f21e5d5f9ebeea949
MD5 3177560ddb41e9c5ac3b4266708049cb
BLAKE2b-256 edbe5564ae059ad0df47628bfd08af7f357d70efffeb1b876f6d6fca8714b9f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 78.8 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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ab49faa6a4a771d763115c99a643c6c45f4c29e42f5caca93e5655c9cf1b91cc
MD5 4353ab5cca034193d51bd7fbf52102b3
BLAKE2b-256 d2d490ad06bddf88d94f7b3b13af61c61cbf497fb5d33e76a48be1f358b0395d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c50397776c05eca52cbe6459ea5789da87bb4bc08677fdc19eead931564e685a
MD5 0dd5b8557dec96245f5e4fdf12c8074f
BLAKE2b-256 1f61f6148827de44fada48cf41500d64e136bc9304dd0f260fdda0188bd11e14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 47d085f108d1c75f4a6830da36641787da2c64326a3b25f15eccbb72c75886f3
MD5 cf948b9d381c637095ecb679a320307a
BLAKE2b-256 d1aed2004a4c497113dd0e58565fcd42232614d799c5cf4b91cfa901968f8683

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 78.8 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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9b63192348bea429847cf49b7030c28de65fa1e11745901c53ebb0d4bd4c3a91
MD5 828452a6ea1443c111c50a8eb3f38602
BLAKE2b-256 320aed3929a9340690e5f9a620d08f0b57e2bb6032fdeffb817ce4baa5f642c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 034c597d0d2c6242ef9ca9566968f1e56e39a9e7c0e1439e8516db549ee37714
MD5 8209760ee94345bbdc5da17bd3b0a39e
BLAKE2b-256 acc919de5b95b629ceb201b5437c2203551fb5fae1dfdd115822ca18ece612f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 65ba8ac59f59dd75f12468d44be2afe00e7257aa11d6fd6cae6b6191a6de83be
MD5 ca4f51a1ec2b0fac0606ffa21ddab5fb
BLAKE2b-256 0fde323bccb4ae61ba8bd928a010f730df256cfd5561948a0363a9c56cd8cc84

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 79.8 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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5344fdf335c4e76245b335aca53c84d42f11fc591f9ba1f15b809110dce19e8f
MD5 7bbc97c73d157a1d35a7e0ae947a1479
BLAKE2b-256 d05d52057dc4d29fe554d5fce9bb7489918061e03c8244254c230c8d7c4c3639

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f2b25a900a6cbb4431f8ddece740b4c083ddadd187a94d0c3104c088ed4f818d
MD5 085adaee9b2383d3064cd23513b39d9e
BLAKE2b-256 4ea058133cc9bb6bcb6cf6b24572c6304d9127c46949bb561236b657c3c3376d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ac81b3151059c6fa87706459e8768f5be645d73306bb90023da75ce9050a7920
MD5 88e6a53509fb00ea75778172e38a0230
BLAKE2b-256 946204259d57814e0272a4559e666aebfe8d56343673ecfdac10ffc885bab866

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 80.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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ec7e14b68eb4346fed65cfe38d04e296652aaf51ce5d411e8c5a5af4ade9c0a8
MD5 8d5c783fa143c36d05d9c0a50cace118
BLAKE2b-256 036a72799c7147d9ba620bae4ad987277325f11c170d991bf2a8260b71654fa9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 153807648a3b42e72287652a03ccc05f1bf44b31e38c0637f58a5c39a150d691
MD5 6b466eb036cf0e2cb34fbd28e5a4addb
BLAKE2b-256 e9876278059ea7f9ebf1dfb6d6427b1a0a2e83a725d5b9546e448dc27e48de03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 62d4446133ae72f7b3574c6ecaf170b672939a7133e10430a6f43534dbf08bda
MD5 1dfcfd9cde58c79b6b868f1701bd4685
BLAKE2b-256 61089ce183782eecf1445f06a307b7d50b614b4bcbea45c2e262f9cde26f6dce

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