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.