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

Uploaded CPython 3.14Windows x86-64

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

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

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

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

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

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

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

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

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

Uploaded CPython 3.12macOS 15.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

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

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

Uploaded CPython 3.11macOS 15.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

ayafileio-0.2.3.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.3.post1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 2459ddc735748cd1559875d0ae1946f628bddff13b3fdfbb9e47a7fd161c79ec
MD5 1065f8aa96f54a8aead229d379e36420
BLAKE2b-256 7810fa2450df1003695c33dc2597415cf87f321d02f77c85eec357f66725c59d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9c0b4223838630f7f5758deaeddf10ae699853f9b539352ded94ec9e6a051e8e
MD5 00e6f1ba06a57b3cd621325cdeef2620
BLAKE2b-256 b08e1edfede90e6bf8a88ae9098f26184c40de9304fa8e68b5ae189a4a3d32c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 61c40ffb8979bc64f37cb2d3b0ed084e3b1c278363cdf9406a3c2cdc7b7fce20
MD5 cf5795fc1ef167b0ed8ca1d55d748321
BLAKE2b-256 794fc028b9e8f33aa0f5acddf9731307fdaea99f22dfc6bcdea2f8198e4e4d8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 df968a6d632b7d9871c9a462d4843f574a0ff8ab04dc60d6f9f5c5639f9a54e6
MD5 6274d392182e9b28f36ae5d690542cfd
BLAKE2b-256 4eb95b4be5939563bd48c81e33649a377cdbb76bce338f4e35811d0e36d36478

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9ad9cf309de0203600f6bdcc228d742fe9d5f9078517036fedff30c95faa450b
MD5 90c082d079040f5f6f18c553585e5359
BLAKE2b-256 569a3f831a7aed3a5bfa57940a01c9f2afa144c0255077b3297dc90b82ba79a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f34828fc002ab63d412f8ea8c58008225b079b4550110f8ca30614882156de72
MD5 809d89abf6c1e6be05045e3b22e0ae4b
BLAKE2b-256 07a7530a3b32c9713cdec3e5a25d1454a3c3b87539ec0cd61ec38fc518059870

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6a92d2cd84c18b219898575ba0d11f42bd881186d5215b4de41d73837a7dd2ce
MD5 ec7c71a9d13d8cd533a66fc2b6bbbfd2
BLAKE2b-256 6178e36abd3740f98e0026b379c7fe72fd0c71045042479c8e16da1f6532619f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e2440a0826c45984fe1ac0bc87bb215207707ea30dd6f8f7d311d053f626e386
MD5 f85b432294e14ea9cd4a5465f0cd6df1
BLAKE2b-256 af188aa27df1d00b7062b4ce133bf4f8fa07a786ae479dee4e3bf11dfedf6f8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 10d0badd6fd79a04af73ba9cb6dee5562a06e0eecf55a49ce7585b78510bfb55
MD5 b395d3872447e3e64d31c42d3cdf3a11
BLAKE2b-256 59678197716d5afb658aa26c49f1e37d77fbc5378d667f44d85c44255235ec5a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f9ebfbdd85af20d515f1b2cc63f8d5c752bea7654b3b4d18a3cedd9275865226
MD5 91aa87aa4c7d8d8c1a8d9619e9777e99
BLAKE2b-256 be35ed43059a79d0a67fc544e87c110b54cad9e677d83067117a7c250dd5ae50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f0184d020c9401e3cbae08f38342b1c2cdd0eb5fdcf089c151ab4ccf29791736
MD5 53aa42e8b8307b5171dabab3ee01d8fe
BLAKE2b-256 d20f584c14318360d00979ce3572f97060c01356d2366df19a76eaa6690fcd7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7aa114d6d1bd251db5877cbc57a1d892bebf425600c37f138a1fd9615a1075db
MD5 8e109b74fd254b6803ac8a19acf2bf4e
BLAKE2b-256 cc84fa9bf1bad5e9265d498126f17e90b9175e58cc60f07b05d94fc20cc9d01c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 89d3ed8ca9a88abe540894911737e157af3bd1f08a5af21ea1969b35b8313c6e
MD5 b7f11af5adfaa5b362af9556bef7f206
BLAKE2b-256 fd7b64f3d375a1af947e3628d8c9b3d49a87e89cc016fa4978ab35c2b7f82d16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bb59df4a6ef97efc125b4511b7b5ecec6c1a393d8513f52556a6b45761742572
MD5 3c47ffd2ed68b7a2cd134ee3d653a8f8
BLAKE2b-256 dfe541a40dadaf979a5dfe27d9283b729095487ee68dc5667e74b09ce390bda4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.3.post1-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 56a110b6bf88cc4f88e1a3e172bde9fce9b957c84d8a7c37b8e0871568fd864b
MD5 57c0798a0148995a59c45bad23e5a3e5
BLAKE2b-256 acb599d553a21b773104f5634c692044c074e70aeb2fddbe68450b8966a492dd

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