首页 > 解决方案 > 有没有 python 异步文件处理程序?

问题描述

我目前依赖于asyncio.to_thread运行阻塞代码,例如文件处理。

有没有更好的方法来做到这一点?

PSaiofile似乎也没有好转。

提供我目前正在测试的内容:

import asyncio
import uvloop
import time
from pathlib import Path


async def aio(i):
    path = Path(f'io_test/async/{i}')
    await asyncio.to_thread(path.mkdir, parents=True, exist_ok=True)
    with await asyncio.to_thread(open, f'{path}/f.py', 'w') as f:
        await asyncio.to_thread(f.write, "print('Hello World!')")


def io(i):
    path = Path(f'io_test/blocking/{i}')
    path.mkdir(parents=True, exist_ok=True)
    with open(f'{path}/f.py', 'w') as f:
        f.write("print('Hello World!')")


async def async_main(n):
    ts = time.time()
    coroutines = [aio(i) for i in range(n)]
    await asyncio.gather(
        *coroutines
    )
    te = time.time()
    delta = te - ts
    print(f'ASYNC IO | Took {delta} seconds.')


def main(n):
    ts = time.time()
    [io(i) for i in range(n)]
    te = time.time()
    delta = te - ts
    print(f'BLOCKING IO | Took {delta} seconds.')


if __name__ == '__main__':
    uvloop.install()
    loop = asyncio.get_event_loop()

    tasks = 1000
    # async test
    asyncio.run(async_main(tasks))
    # blocking test
    main(tasks)

预期与否,“阻塞”过程相当快(2 倍甚至 3 倍)。我不能不使用 async 的原因是我真的无法承受流程中的阻塞点。

标签: pythonfileasynchronousio

解决方案


推荐阅读