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

Uploaded CPython 3.14Windows x86-64

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

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

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

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

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

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

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

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

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

Uploaded CPython 3.12macOS 15.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

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

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

Uploaded CPython 3.11macOS 15.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 9d1a3d4a8364d8e28cc5ab847ecf932c613198e36727ca6f8bcf0dd86518c739
MD5 b7d0bcd028f3a37ec7233afd5d95b082
BLAKE2b-256 ac51a8ed579b5abe435931ef08b7eba8f3844a516c525710a24fe111f2fe71bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 24a502c297dc1bd27dcded906674c1ca07ea62aec836f905f079f9395e7cbdfb
MD5 75c998d0431b48bfc701a16c855a0a8f
BLAKE2b-256 bbd9d37fa23dc714be31727be9596af06efb996915595a416389105b249cdee2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7395ab8ed5240590b48f52122d5fbb1e8dfee20045e0ae686bfbc3f925e5ba9a
MD5 1b1258ae08052bf5ceb59c38bc6013c0
BLAKE2b-256 2d3387ca45c7c6cd584401fda2fc75b32b090c9bd303bd4600e072ce384b53d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f055661de4391dc59840a949b3ac5ad71a35166c3d5fb1b8f7b3ce3e4395f726
MD5 0f8cd865217824937de87e3d262668e1
BLAKE2b-256 26a58c48b74e7a698e76a20c7e3a35d98fd8852af828a25448bfa41a4bb0fa19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0431735dd8366a620a133a68a557b418fe9e02df6b992500585b4cb2ede7bb2c
MD5 678395ac4f9721be35d15b0104613a4f
BLAKE2b-256 8bfb456de9096830fe9725a0793dbefb4b936b5a4fe0e44adb4006eaf06251f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0f430d0a8bb5a347d4a60e843e1a21bd910da75083623ad9bd67c08b207fc1cd
MD5 c7860d93444bfe050d8d3b6e9719a6d1
BLAKE2b-256 153adbf4493a53f8b81bac6d2435bcbed297b28db523e856cd5a88cc4a7cb269

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 57381fe4b2daf6045380b24925305376b541e1dd3dfae9716f611e64f410748e
MD5 36f6ee1b0ded6036891c8c72b773ac8f
BLAKE2b-256 d4ac7bc61a1b36bfe55a39f850616136d30d01f8d04becc60b5357b1c351ac54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 267ef7a11f6b4d5d8d8f48da3235305fa39f16bae5ecd645c76eed62385d3f01
MD5 9c98ee3e48e481a4aac50241e19e8ee5
BLAKE2b-256 b11e0c7e3bcd8f486450afa05b6854144eab4d86dc97118c38ff60201ac867b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 30d61ec220878b749fd23d0df0d51ea2800bd66d5e0ae68758bb369980ffe4c9
MD5 9168acbf5d871d9fe4b18f2498649f9b
BLAKE2b-256 575f111737fc116eb330ce0d2539bb70071777761fa732b4594e4ee857223fc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7266957ae5969b903efd60d60bc371fd4643b1d7e021ce557ae472873642da56
MD5 63fbbc44547d1f5ca4a83ec9545b5403
BLAKE2b-256 c818fb47173e94b6c24233e7aace2ff566ecd533db62f8c27d2b2d4e39c0c766

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d2df460fb1f0c93472ee6cc5d589d41e4268de5d90e3cea9f75334c6e0f6c81f
MD5 aa99f72d9504010505a6f595e9f72e13
BLAKE2b-256 cdbff64aff53b860180a6bbb94d52086b0a5a3314cd1ae80ef7c2185358efba9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 96738bb0274cc16c28a7b6ea53b2b8b458b448406418cff8bbf39d253cb144cc
MD5 4e7a4a5927078532ad0b610257bd24d8
BLAKE2b-256 8d52d952a81feedb94b1dd78a3572f93689a20466dc8f804fd593a64dbf30b81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f02de05ebca9ce06971d01f2e854e2b513f0f6092ed8234cd20ca686f411c779
MD5 bf9855345353a510609457551c19172d
BLAKE2b-256 b3a657c546df8f4a8ea7e10b87c4547ac1c0041d074dcd0e7c09e99d8ca1ba8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ac2168637e0d1a3eab1c74d9702b834566862151962ef666cd8619693f9f4b64
MD5 094da5ca3688ee0fb2bb1ae9dc8acb42
BLAKE2b-256 69b249ca1d8bd30e2ac5b3af409f09dba3dcda6099865e1b34d1419224b8b862

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post2-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 c29f889feed5daae32cac37b1c67239d7b2afe80fb788bd5ee4a838c258dc37f
MD5 1f800c8415440078c91f74b2678ba2b2
BLAKE2b-256 7e2a839f05a61baf4759c5501e6912080b531f810f3b0364c63a46b6b5ceb194

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