/aiofile

Real asynchronous file operations with asyncio support.

Primary LanguagePythonOtherNOASSERTION

AIOFile

Github Actions

Latest Version

image

image

image

image

Real asynchronous file operations with asyncio support.

Status

Development - Stable

Features

  • Since version 2.0.0 using caio, is contain linux libaio and two thread-based implementations (c-based and pure-python).
  • AIOFile has no internal pointer. You should pass offset and chunk_size for each operation or use helpers (Reader or Writer). The simples way is use async_open for create object with file-like interface.
  • For Linux using implementation based on libaio.
  • For POSIX (MacOS X and optional Linux) using implementation using on threadpool.
  • Otherwise using pure-python thread-based implementation.
  • Implementation chooses automatically depending on system compatibility.

Limitations

  • Linux native AIO implementation not able to open special files. Asynchronous operations against special fs like /proc/ /sys/ not supported by the kernel. It's not a aiofile`s or `caio issue. To In this cases, you might switch to thread-based implementations (see troubleshooting section). However, when used on supported file systems, the linux implementation has a smaller overhead and preferred but it's not a silver bullet.

Code examples

All code examples requires python 3.6+.

High-level API

async_open helper

Helper mimics to python python file-like objects, it's returns file like object with similar but async methods.

Supported methods:

  • async def read(length = -1) - reading chunk from file, when length is -1 will be read file to the end.
  • async def write(data) - write chunk to file
  • def seek(offset) - set file pointer position
  • def tell() - returns current file pointer position
  • async def readline(size=-1, newline="\n") - read chunks until newline or EOF. Since version 3.7.0 __aiter__ returns LineReader.

    This method suboptimal for small lines because doesn't reuse read buffer. When you want to read file by lines please avoid to use async_open use LineReader instead.

  • def __aiter__() -> LineReader - iterator over lines.
  • def iter_chunked(chunk_size: int = 32768) -> Reader - iterator over chunks.
  • .file property contains AIOFile object

Basic example:

Example without context manager:

Concatenate example program (cat):

Copy file example program (cp):

Example with opening already opened file pointer:

Linux native aio doesn't support reading and writing special files (e.g. procfs/sysfs/unix pipes/etc.) so you can perform operations with this files using compatible context object.

Low-level API

The AIOFile class is a low-level interface for asynchronous file operations, and the read and write methods accept an offset=0 in bytes at which the operation will be performed.

This allows you to do many independent IO operations on an once opened file without moving the virtual carriage.

For example, you may made 10 concurrent HTTP requests by specifying the Range header, and asynchronously write one opened file, while the offsets must either be calculated manually, or use 10 instances of Writer with specified initial offsets.

In order to provide sequential reading and writing, there is Writer, Reader and LineReader. Keep in mind async_open is not the same as AIOFile, it provides a similar interface for file operations, it simulates methods like read or write as it is implemented in a built-in open.

The Low-level API is fact is just little bit sugared caio API.

Reader and Writer

When you want to read or write file linearly following example might be helpful.

LineReader - read file line by line

LineReader is a helper that is very effective when you want to read a file linearly and line by line.

It contains a buffer and will read the fragments of the file chunk by chunk into the buffer, where it will try to find lines.

The default chunk size is 4KB.

When you want to read file by lines please avoid to use async_open use LineReader instead.

More examples

Useful examples with aiofile

Async CSV Dict Reader

Troubleshooting

The caio linux implementation works normal for modern linux kernel versions and file systems. So you may have problems specific for your environment. It's not a bug and might be resolved some ways:

  1. Upgrade the kernel
  2. Use compatible file system
  3. Use threads based or pure python implementation.

The caio since version 0.7.0 contains some ways to do this.

1. In runtime use the environment variable CAIO_IMPL with possible values:

  • linux - use native linux kernels aio mechanism
  • thread - use thread based implementation written in C
  • python - use pure python implementation

2. File default_implementation located near __init__.py in caio installation path. It's useful for distros package maintainers. This file might contains comments (lines starts with # symbol) and the first line should be one of linux thread or python.

  1. You might manually manage contexts: