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

Uploaded CPython 3.14Windows x86-64

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

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

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

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13Windows x86-64

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

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

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

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.12Windows x86-64

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

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

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

Uploaded CPython 3.12macOS 15.0+ ARM64

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

Uploaded CPython 3.11Windows x86-64

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

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

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

Uploaded CPython 3.11macOS 15.0+ ARM64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

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

File metadata

  • Download URL: ayafileio-0.2.4-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 83.4 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.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 51dd8236a070bb4978c9ae8dc67a8758ab100581e8a42c99b6e2d665046d4411
MD5 5d992e50685d84c73be3028e91b41325
BLAKE2b-256 b322b4673f4c618850d335b69881402e3c05b3f9f670728033316cd23e3a3254

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8d65332df40cee902b38b2f5babb1f53f420f2c842935b02d29474e78d94d55e
MD5 578d71945c92dba772c31b28c7357f29
BLAKE2b-256 23b97e63ffbaa29dba7aff19dccbd8f3a362a1831cc1bc5a50944c39b451e658

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 a095145fc944904e2c6096c0602d84e8bfd3f69ca3420aac1b0111e7bcff7922
MD5 b674b893b4cd01667d2a0c6ba3be9844
BLAKE2b-256 a85771cead108739df4fe8cc54e1e64751a5f79ac6a1bee46b8ce9120176a66f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 81.1 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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fa7d458f309b79992260183edd61a62d3dd96166ee79b73167a2c243bd056298
MD5 a644c89bf8ca37576920c845a4d7882d
BLAKE2b-256 46c01d5f77f54a5cdc7c0043acfbce89e2c4eea7462c725c45e0a45e72c738dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1a8f0ce826207ca0db2ab8238224f3c786b912f8f3670c04e25c3689bb9d785b
MD5 db83803880c7ff90e158736d9488f521
BLAKE2b-256 fc2a66c8d2116250258cfddf92487d9eb9ba8beb41b8d1695be0631b4af5cf99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 fb2e3ccfe568fa0e996863ec45682c7935061b9e11e267dbf3e86c715a0426a6
MD5 86a5176991bd2acc1d24289644c1cd39
BLAKE2b-256 f1aeb2e9c5ecf7b21501dfe95cb0b5b436e26cd4f2420ae70b4ee5bb10e068a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 81.2 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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 347849a227c0b4da84366bce99c6fb01a8ae1004480bd6a3bb5b44f58d3ee02b
MD5 3485943f8b12236152f893b0ea0b706a
BLAKE2b-256 5945ff253722b188b522bcef43b49977f4576d2f9c412ad858aa8f6d6e15091d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f29a22731f81e332f91648a0c02dfdab8874618c1f1dc3361e94eb7b8ea23090
MD5 3a1cd0e722eada8ea92bbecd05234361
BLAKE2b-256 e24380adf2cdec8a81b3bac7a788e0cb121c4799dfc29716e0444a61a5f9c5b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7db46dec635e745f2ab3ff06601ce4e8edb70f3cd8939acecd054b11b5e1127c
MD5 03c3ef6cd3fc4116c2efa3fe943519b8
BLAKE2b-256 326cccbce0e60e1b2320a112c82be112a229f36001dcd9362b349a9aaa4024e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 82.1 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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7e75347e346bcb3808e6e2acb6135c69c717afb50f4d7c1fbb4da117e2ce0ab0
MD5 e892a536646cdd1d1033697e28bc6929
BLAKE2b-256 b8ca85aff03acd1e5254f1821914b22161f125d58554b78b1413f3cfc4a8b9df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f8be6c472ba708c78c4076a6b22ff8b3df2f443f40a756238c76ade512a8c1bc
MD5 89d3248c9e77e37057d62597972ed848
BLAKE2b-256 a1a0f27f83ea23444a98cc591a1d28b14f2bb1d4182e9942af2dce8ab24a798f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 1e247a65ee4a5e4cafcffdc6a7ee8361a61f95d5af958094f6a55b1b1a8d00f0
MD5 b09574d06a5b214ce5753a7cdc6872a7
BLAKE2b-256 570d18a527ae083b06c32db58282bf3234c472d76c18e0e514c1ca861daa1541

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ayafileio-0.2.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 82.2 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.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d5b9c6992caeb7816d6ab70940ec62a6b944efa1c85e553d1bf3a01c3ce6723e
MD5 37b10143626a39415b0557c990370c22
BLAKE2b-256 0c363d2bc5e19e8f8765b8749e76cffa77c9983f9ed287fc2aec21929333724e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 10da39fbc50aa1cc1f623ae69f4dd3325d26c86ebd0ea27d23345c389520b605
MD5 bf8131e2bdba15d9f1aad0a97816b126
BLAKE2b-256 7414b985be4e652b27c0314c2691e20caf7c4f0a43b9f3cace372215a42fd7ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ayafileio-0.2.4-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 451ccf6b7a18fd8fb16bcc67b7f56661c11435b0b627184dae37722f87537b91
MD5 05c40563ff17f2a85421c7cd3e0f8264
BLAKE2b-256 2a6da346d84cc2bd78efdc260a943ccb383f09af764a352af62f0194878faa47

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