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

Uploaded CPython 3.14Windows x86-64

ayafileio-0.2.2.post1-cp314-cp314-musllinux_1_2_x86_64.whl (542.2 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

ayafileio-0.2.2.post1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (81.2 kB view details)

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

ayafileio-0.2.2.post1-cp314-cp314-macosx_15_0_arm64.whl (61.4 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

ayafileio-0.2.2.post1-cp313-cp313-win_amd64.whl (78.9 kB view details)

Uploaded CPython 3.13Windows x86-64

ayafileio-0.2.2.post1-cp313-cp313-musllinux_1_2_x86_64.whl (542.3 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

ayafileio-0.2.2.post1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (81.3 kB view details)

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

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

Uploaded CPython 3.13macOS 15.0+ ARM64

ayafileio-0.2.2.post1-cp312-cp312-win_amd64.whl (78.9 kB view details)

Uploaded CPython 3.12Windows x86-64

ayafileio-0.2.2.post1-cp312-cp312-musllinux_1_2_x86_64.whl (542.4 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

ayafileio-0.2.2.post1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (81.3 kB view details)

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

ayafileio-0.2.2.post1-cp312-cp312-macosx_15_0_arm64.whl (61.5 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

ayafileio-0.2.2.post1-cp311-cp311-musllinux_1_2_x86_64.whl (543.5 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

ayafileio-0.2.2.post1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (82.3 kB view details)

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

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

Uploaded CPython 3.11macOS 15.0+ ARM64

ayafileio-0.2.2.post1-cp310-cp310-win_amd64.whl (80.1 kB view details)

Uploaded CPython 3.10Windows x86-64

ayafileio-0.2.2.post1-cp310-cp310-musllinux_1_2_x86_64.whl (543.5 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

ayafileio-0.2.2.post1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (82.5 kB view details)

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

ayafileio-0.2.2.post1-cp310-cp310-macosx_15_0_arm64.whl (62.7 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file ayafileio-0.2.2.post1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 586806c2f8c0b1a992583f849d4296c4f25fcf24f2c313038f3d46238471aa48
MD5 fd7154632526a59a96ae66c644aee8bf
BLAKE2b-256 1780a344c613924253975755479aa8595215e9d873e4813d13b249a01a836446

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1820f800c5e7ce7c8daa45dda1046d84054240b3ed96ff5801ae955ee6707174
MD5 fa51ac44de8e33538df0e7f3173d8c59
BLAKE2b-256 1c45f00d5e4f7d288914fbeecfc06b4bbede4ffdcd89a1b4039159c2562441f3

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c6797aaf26d681578ea43ba85f8ca51c4a71f211c7a4a7366d033e2c96332fdf
MD5 f7f6a8d251f16bc7786058abfd08ea06
BLAKE2b-256 f0d445257acb4d41fc8b8f288f4fc72a3ce289cf4241e3bc625cdc66cf8fbb4f

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d74021e1cd9fb04364ca729ae305beb062aac446a0594bc8e43d1b4713edf2c9
MD5 1b5177a448d9fbd142c218d98c5d7d4c
BLAKE2b-256 8db140249a8d598c24eedc812b8c1a07081a0741aab2a2e2ed3d810f243d2131

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c261c9cc0ee76f4c1b7714f7f71a2026540bcd7ba71ca80749ae820510b6ca9c
MD5 1b7f46d7f14dae072d40371a00cefd68
BLAKE2b-256 f365cee7e19d5f049b8e7f45b52084c81e56c7d940ffa900c1938b1ca9665038

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d4b249a349151d3126923069306eadd500dc5cadb2afdc5d474e958a68024a18
MD5 65c51cdef35abef8e3b965b66b34bf25
BLAKE2b-256 8c0f7e97258107f1befab012dc7d05af98f4b4ef2ffc5e224adc96bb7447c2f8

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e739cc6864e23bae30948ed812fef01e9830acc1c9bf117ba3e06221a453751a
MD5 bb053341ac39c65661f9164c982a8433
BLAKE2b-256 add266be33d484fd5da3def21d336b336a7836fbbb6435791a17dda1e4842bf8

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0aa63697e298608783edb654eff43fd29b48b32f9102ddf17ce3721b3b57a63e
MD5 445f264dc28d6981440cb6668c607aa6
BLAKE2b-256 f5d1e009f81099fe5d30bef1cc98f9f55794411dd32f0083ebdc84efd144ac65

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bc2613d6f770288221ec66872e84c3c44c588c7ae2a8cb186309cdd5873c653e
MD5 a71c55f0bb5253ac75ccaf1bb17e6c69
BLAKE2b-256 7231f65c936020f7db339725943c863fdf5f75feaba6b57f0ee64b7d54369f51

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 46e17d0cf50d114d3043b0f23a5ecd31db50b7fd10796b0f55c8c18befcc2641
MD5 0042dfe2a54e030e7be48806cfaf45e8
BLAKE2b-256 374d6c6e537b8036630bc62787dadadbf0dbea573903d7100cb6fc7e7e68245d

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ed8ead61d58665e603d238e7d002952f02cd5b18c1f92dfa7a4c9c7e5e7fe18c
MD5 219d9b1ab6087474fc9e8c4dc8038467
BLAKE2b-256 12bf59bc26794c96fb136a95596c0d1c68d535cd2ccb09facf34792c7f478f3d

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f3ae4e4c0c7da9a80b61290e1101e665daa6a350dc3ca231fcdb486c7d9cb384
MD5 ac20c3990adf787a50199383b269c27d
BLAKE2b-256 83fb20720cfca5fc5034865f60214448c26f10d15214c9705c5cceba901dc43f

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 54584e5ccb10c3e5e17b6e09a461b6923006b17d64237b8190bf267ed8b5d903
MD5 8db83086e40e920e328364103d451ffa
BLAKE2b-256 eb768af5589ea5b5e6ec972ef3d7bf4796883ae834a63168c76346762b17e484

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f0b657944321b7e19d9f35cd8d2f45d10c4d90e16ed9c99865d52f52d4d3d189
MD5 a8c6f2d75bf2e7d4c5af97653a676b52
BLAKE2b-256 3e56c21d27dd8ea186ea41e09632db2254e5ae213b877faf8fdc297061c147ff

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cd9ba8f06aa0474aed8bde145bfb2c503a8512991da6edbf7a5dfbc70d6cd672
MD5 7dbb041116c488d11efa4ce7bf8d7d7f
BLAKE2b-256 a70883df9932f3d1a94143471fcb2599bd0eeb09cb7172610b44afd00a01c29e

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7705ec987160d65520a7b043c28106631e8ddec121d8ee94bd5276f22464946a
MD5 1e824af69e016220036650fa6a5195db
BLAKE2b-256 f9b77f813b2487ddfb12b8371efcb739341651e208ce97fa977076fcfbf846a9

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 aecdc53fa68e84a115aa40a59e2eadc0f392285e9e2b084db12889a465dae30e
MD5 ae5b3c7a971d22706dc35f4ce1fc4d6d
BLAKE2b-256 62d19c1eb647599c082047305005dec6d0f208565fecbc059017242a86642ed4

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cc0d901e4d0248cd03c1dfea00c48256ed25cb09bca5ea81a57ae27b48e2d0cd
MD5 bffc153c3edccfb76461e061af6b1499
BLAKE2b-256 7b03690c9cad51210164a6ec4164218bbd653df1acd84db6dc0d5c9c09515aee

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 516dbc13d90da2ac2fedb0d2ee2409104abf5a8f664405adc0a4e09b61859ac4
MD5 03ae848771add13313c18691cbac54f3
BLAKE2b-256 671c903cf6b94ef9a8731e94a70342190ed4c6e53f099678b5c2fa0dd5feffa2

See more details on using hashes here.

File details

Details for the file ayafileio-0.2.2.post1-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.2.post1-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 addb27b2c6ea99dfe420a855d75a53a21c61172f95e8ff64cd361abd02470757
MD5 bec58519ba4859511fe47d55e135bddc
BLAKE2b-256 a37f1648a2413dbe94863dbd0c640e9531037c72986f8773faa4cd5a90b615f2

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