-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdisk_io_test.py
More file actions
45 lines (29 loc) · 1.03 KB
/
disk_io_test.py
File metadata and controls
45 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import asyncio
import aiofiles
def _create_large_file(file):
print(f'Creating a large file @ {file}...')
contents = '\n'.join([f'this is line number {i}' for i in range(10000000)])
with open(file, 'w') as fp:
fp.write(contents)
print(f'Wrote {file}!')
async def read_file_async(file):
print(f'Starting to read {file}...')
async with aiofiles.open(file) as fp:
contents = await fp.read()
print(f'Finished reading {file}!')
return contents
async def arbitrary_async_process():
while True:
print('If I show up between "Starting to read ..." and "Finished reading"..., disk-IO can and should be awaited.')
await asyncio.sleep(.1)
async def main():
file = 'a_big_log.log'
_create_large_file(file)
read_task = asyncio.create_task(read_file_async(file))
other_task = asyncio.create_task(arbitrary_async_process())
await read_task
print(f'Removing {file}...')
os.remove(file)
if __name__ == '__main__':
asyncio.run(main())