import aiofiles
import asyncio
1、异步读取文件
async def read_file():
async with aiofiles.open('/etc/ssh/ssh_config', mode='r') as f:
contents = await f.read()
print(contents)
loop = asyncio.get_event_loop()
loop.run_until_complete(read_file())
2、异步写入文件
async def write_file():
async with aiofiles.open('test.txt', mode='w') as f:
await f.write('Hello, world!')
loop = asyncio.get_event_loop()
loop.run_until_complete(write_file())