Skip to main content

Async Windows IOCP file API for Python

Project description

aiowinfile

License Python Version Platform

Windows 平台真正的异步文件 I/O 库,使用 IOCP(I/O 完成端口)。

与传统的线程池方案不同,aiowinfile 利用 Windows 原生 IOCP 实现内核级异步文件操作,零线程开销。

🚀 核心特性

  • 零线程开销:无需 run_in_executor,无后台线程
  • 内核级完成:无用户态调度延迟
  • 高并发友好:轻松处理数千并发文件操作
  • 标准 API:熟悉的文件式接口,支持 async/await
  • 文本二进制支持:文本模式自动编解码
  • 可配置句柄池:根据工作负载调整性能
  • Windows 原生:针对 Windows IOCP 架构优化

📊 性能表现

实际基准测试显示,相较于线程池方案有显著优势:

并发数 aiowinfile (ops/s) aiofiles (ops/s) 加速比
10 688 1,166 ~1.7x
50 2,972 1,320 ~2.3x
200 2,981 1,244 ~2.4x

Windows 10、HDD 存储上的基准测试结果。更高并发显示更大优势。

🛠️ 安装

pip install aiowinfile

系统要求:

  • Python 3.10+
  • Windows 7 / Server 2008 R2 或更高版本
  • 无其他依赖

🚀 快速开始

import asyncio
import aiowinfile

async def main():
    # 基础异步文件操作
    async with aiowinfile.open("example.txt", "w") as f:
        await f.write("Hello, async world!\n")
        await f.flush()

    # 读取并自动文本解码
    async with aiowinfile.open("example.txt", "r", encoding="utf-8") as f:
        content = await f.read()
        print(content)  # "Hello, async world!\n"

    # 二进制操作
    async with aiowinfile.open("data.bin", "rb") as f:
        data = await f.read(1024)
        await f.seek(0, 0)  # 定位到开头

asyncio.run(main())

⚙️ 高级配置

句柄池调优

对于高并发工作负载,调整句柄池大小:

import aiowinfile

# 查看当前限制
max_per_key, max_total = aiowinfile.get_handle_pool_limits()
print(f"当前: 每键 {max_per_key},总计 {max_total}")

# 增加以提升多文件性能
aiowinfile.set_handle_pool_limits(128, 4096)

这会在打开/关闭周期中重用文件句柄,减少昂贵的 CreateFile 调用。

📚 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 set_handle_pool_limits(max_per_key: int, max_total: int) -> None
def get_handle_pool_limits() -> tuple[int, int]

🧪 运行基准测试

克隆仓库并运行基准测试套件:

git clone https://github.com/your-repo/aiowinfile.git
cd aiowinfile
python run_benchmark.py

这会在不同并发级别比较 aiowinfileaiofiles

🤝 贡献

欢迎贡献!请:

  1. Fork 本仓库
  2. 创建功能分支
  3. 为新功能添加测试
  4. 确保基准测试仍通过
  5. 提交拉取请求

📄 许可证

MIT 许可证 - 详见 LICENSE

⚠️ 限制

  • 仅限 Windows:使用 Windows 特定 IOCP API
  • 不支持 Linux/macOS:平台特定实现
  • Python 3.10+:需要现代 Python 特性

🙏 致谢

基于 Windows IOCP 构建真正的异步 I/O。受 Python 高性能异步文件操作需求启发。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiowinfile-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiowinfile-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file aiowinfile-0.1.0.tar.gz.

File metadata

  • Download URL: aiowinfile-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for aiowinfile-0.1.0.tar.gz
Algorithm Hash digest
SHA256 56ff557685e7e0e52781ea4281570bacc8d0f61903ade5e3a2569d064282298b
MD5 eee7161d56b6c90f66f388ea56534cc4
BLAKE2b-256 04d58613f0626ccf595c4f3d388a823f8eb8a4393990a11b6d0f91edfd16214e

See more details on using hashes here.

File details

Details for the file aiowinfile-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aiowinfile-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for aiowinfile-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43d6473ff7f893ef4623a47c0e67ccd0371768595c5d6a74b27ed8b8af7fc505
MD5 c9679e17a10ee77b8fb201381e154cff
BLAKE2b-256 bf7a16609d1836f1ac5f476970a89b61affb0f5172985e66dc18ad4d44b38953

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