Last active
April 28, 2021 00:09
-
-
Save pangyuteng/728adcddb1d506f6a5d3a39650a1b76d to your computer and use it in GitHub Desktop.
profile io with asyncio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.