File support for asyncio.
Maintained. Last release 14 months ago, commits in 7 of the last 26 weeks. Weaker on recent releases and release cadence.
pip install aiofilesaiofiles: file support for asyncio
aiofiles is an Apache2 licensed library, written in Python, for handling local disk files in asyncio applications.
Ordinary local file IO is blocking, and cannot easily and portably be made asynchronous. This means doing file IO may interfere with asyncio applications, which shouldn't block the executing thread. aiofiles helps with this by introducing asynchronous versions of files that support delegating operations to a separate thread pool.
async with aiofiles.open('filename', mode='r') as f:
contents = await f.read()
print(contents)
'My file contents'
Asynchronous iteration is also supported.
async with aiofiles.open('filename') as f:
async for line in f:
...
Asynchronous interface to tempfile module.
async with aiofiles.tempfile.TemporaryFile('wb') as f:
await f.write(b'Hello, World!')
Features
- a file API very similar to Python's standard, blocking API
- support for buffered and unbuffered binary files, and buffered text files
- support for
async/await(PEP 492) constructs - async interface to tempfile module
Installation
To install aiofiles, simply:
pip install aiofiles
Usage
Files are opened using the aiofiles.open() coroutine, which in addition to
mirroring the builtin accepts optional and
arguments. If is absent, the default loop will be used, as per the
set asyncio policy. If is not specified, the default event loop
executor will be used.