Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Last active April 28, 2021 00:09
Show Gist options
  • Save pangyuteng/728adcddb1d506f6a5d3a39650a1b76d to your computer and use it in GitHub Desktop.
Save pangyuteng/728adcddb1d506f6a5d3a39650a1b76d to your computer and use it in GitHub Desktop.
profile io with asyncio
import os
import sys
import uuid
import time
import numpy as np
import asyncio
import aiofiles
from aiofiles.os import wrap
# make sure any io operations use the async version
copyfile = wrap(shutil.copyfile)
movefile = wrap(shutil.move)
file_exists = wrap(os.path.exists)
makedirs = wrap(os.makedirs)
np.random.seed(0)
#root = '/mymount/tmp/ok'
#root = '/tmp/ok'
root = sys.argv[1]
getdata = lambda x,y: (255*np.random.rand(x,y)).astype(np.uint8).ravel().tobytes()
async def myjob():
uid = uuid.uuid4().hex
n = 2
uid_path = [uid[i:i+n] for i in range(0, len(uid), n)]
myfile = os.path.join(root,*uid_path)
os.makedirs(os.path.dirname(myfile))
x,y = np.random.randint(700),np.random.randint(700)
data = getdata(x,y)
async with aiofiles.open(myfile, 'wb') as f:
await f.write(data)
m=100
n=10
async def mymain():
for _ in range(m):
results = await asyncio.gather(*[myjob() for x in range(n)])
await asyncio.sleep(0.1)
if __name__ == "__main__":
stime = time.time()
asyncio.run(mymain())
etime = time.time()
print(etime-stime)
print(f'expected file count {m*n}')
cmd_list = ['find',root,'-type','f','|','wc','-l']
print(' '.join(cmd_list))
@pangyuteng
Copy link
Author

pangyuteng commented Apr 12, 2021

executed in docker.
ubuntu 18.04
miniconda python=3.7.2
pip install https://github.com/Tinche/aiofiles/archive/master.zip
0.7.0.dev0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment