0

I am currently relying on asyncio.to_thread for running blocking code, such as file handling.

Is there any better way of doing this?

P.S aiofile doesn't seem to be doing any better either.

L.E

To provide what I'm currently testing:

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)

Expectedly or not, the "blocking" procedure is quite faster (2x even 3x). The reason I can't just go without async is that I can't really afford having blocking points in my flow.

  • Please provide a minimal reproducible example and explain the issue you are having with it. – Klaus D. Oct 12 '21 at 08:59
  • Does this answer your question? [Does asyncio supports asynchronous I/O for file operations?](https://stackoverflow.com/questions/34699948/does-asyncio-supports-asynchronous-i-o-for-file-operations) – MisterMiyagi Oct 12 '21 at 09:02
  • The TLDR is: The problem is the OS, not the library. ``aiofile`` (not ``aiofiles``) *does* use ``libaio`` if available. – MisterMiyagi Oct 12 '21 at 09:04
  • @MisterMiyagi yes, that answers my question. Thank you! – Adrian Lazăr Oct 12 '21 at 16:42

0 Answers0