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

Uploaded CPython 3.14Windows x86-64

ayafileio-0.2.4.post1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (104.6 kB view details)

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

ayafileio-0.2.4.post1-cp314-cp314-macosx_15_0_arm64.whl (65.1 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

ayafileio-0.2.4.post1-cp313-cp313-win_amd64.whl (81.2 kB view details)

Uploaded CPython 3.13Windows x86-64

ayafileio-0.2.4.post1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (104.8 kB view details)

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

ayafileio-0.2.4.post1-cp313-cp313-macosx_15_0_arm64.whl (65.2 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

ayafileio-0.2.4.post1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (104.8 kB view details)

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

ayafileio-0.2.4.post1-cp312-cp312-macosx_15_0_arm64.whl (65.2 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

ayafileio-0.2.4.post1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (105.6 kB view details)

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

ayafileio-0.2.4.post1-cp311-cp311-macosx_15_0_arm64.whl (66.2 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

ayafileio-0.2.4.post1-cp310-cp310-manylinux_2_28_x86_64.whl (105.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

ayafileio-0.2.4.post1-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.4.post1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d2479ec95e267e3001a6fbc64177d7479a9c8882f5eb8b6fd76f370ff224e76b
MD5 9478b5d83d8737525ab2de9759521199
BLAKE2b-256 2a07de94e1803c77b07081aeffa766a4d6f7a026a3567a86aaf812b103f55fa1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7e7d736d3b936bdd759846180c86c23d1ee787071911cb6ebc69650c811d484d
MD5 c035d580e99ff01fb441436c77628c62
BLAKE2b-256 fefcf00ec372f25a3debf61a8d4b394590b8032865189d86a55c1cf0c85d28c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 aa0f7931015754ab6394ee0d26d8e016d59c2cc23a0358685d73e9b1c096acb7
MD5 52fff54df02de23f924967428a48608f
BLAKE2b-256 6e6713924d909ba769204e3e53aa6a24e5e0ab1f595578c2cf14f6693751427b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bf91d7ba72f66bdd72838cd4c62528e285297b0eededba401b2ae8edfec6d939
MD5 070393ed3ee4c4b2d65c36b82d4c860f
BLAKE2b-256 9bb53390c4f7a9a8892bd28e3e28584a13f667f2546e7a0a96ed498c201e6b01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bafb5376da1be4f41bb2149c84ec9c67f0ad8b7cedf24bf079404f7531319563
MD5 fd67f6c59ff674f642490db3c9f18312
BLAKE2b-256 b065802dbcc6c37f1ec6f9f7c588153da91ed4648dcc3f9be328ec1a5529e29c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 4c85fb0797d0a4d1339a9675435cba3b3a88ffe953fd61db5a74c13a2c5c15e5
MD5 a7ac48532c3b6dec0889cc3e513bb0fa
BLAKE2b-256 640493466fba9eb879aa6843c77bb40889e1459c191bc6ff3255a798e26cad2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 402d9dcef0d08b1ea5bf56384cfe508206956c207f897d34184153d8f1020331
MD5 9311753a6f38c0909698efa268a5b84c
BLAKE2b-256 1c1396ec82820a10052a3b8e97292ac4e0704cdf496631e60428933e569d4448

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6d6c3c7c0679992e9ae3ef7fba6a7d98fe2f1ae545c97e6b83422f0f861da691
MD5 1332b1b7f6d514b7edab9140761048f0
BLAKE2b-256 6e5c8637e782e498d9a19b02b20d699452cb55f51fadb7921ddf7116a8bffdad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 69c1d54bd4102c65f870e8ef9f8495cda0231cc700d60b8344497ab06a48e6cd
MD5 075584319d5a20ab40f8949228d0b60c
BLAKE2b-256 30d08ba3870fcf3d68b62e406465e2106048e68968363f920b75bf1d104eb480

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 93a772162f41887f8a9a69fb138226ac2762441fbcfcb957e8265a03beedf985
MD5 9e0e3e2634204b6c96ee414e1d4eaa3b
BLAKE2b-256 77c1a4a65c7555d5d2d8b0906ba238f23a14be62a66d30f4412de442d180fadc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b7eba66c841a02d3b017792f8f5ed8282458dca6251f471a02b62d3c58116169
MD5 2ec28800dfe54921f5b327d33f964364
BLAKE2b-256 4784cc4bf09da789ee16934c46e2ea827213f7e65f30d490c80c13060e9183d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b5d309f8697f5545aff250853565a2c7ce3f8488fccce84386d621562284db5b
MD5 c61a8e6150a678b6050536389d2d78cd
BLAKE2b-256 529fe5a1f0bf8a2a2bde70c1f19243bd22df10038de8f360682e99184e01a574

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 477bdeeab40514b0ed62f515efb369e11efca015d70ff518d5ede7cb29508041
MD5 3f0df2e0c43bec92cf6982143965c200
BLAKE2b-256 887e03a569f13294efe2e2a93eb58e1c5c3995b48e34c64d3ec065141be58ae4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c191fb985c3db06382a681042c8b50d629f72da8fc74377824280a08fc26a707
MD5 e7da6f788729e6b1fa0296c8d1c50d7d
BLAKE2b-256 e28dd7162942eafcd4def88f27e273919cd819ba9edd997cf0c48bdd175c75a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4.post1-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b8591a0af76c525ba5943b8c8a26e9b04af9b1ef95d32de1954e938f9a0fc37c
MD5 13e1a11523d1f93f9ca8e692b4082dad
BLAKE2b-256 ecfa09badbfb5bc0fc9d3f6f4a312c7757d020a68601fbdc3c4e0d93c358e24e

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