Skip to main content

AIOFile

Travis CI Latest Version https://img.shields.io/pypi/wheel/aiofile.svg https://img.shields.io/pypi/pyversions/aiofile.svg https://img.shields.io/pypi/l/aiofile.svg

Real asynchronous file operations with asyncio support.

Status

Development - Stable

Features

  • AIOFile has no internal pointer. You should pass offset and chunk_size for each operation or use helpers (Reader or Writer).

  • For POSIX (MacOS X and Linux) using implementaion based on aio.h (with Cython).

  • For non-POSIX systems using thread-based implementation

Code examples

All code examples requires python 3.5+.

Write and Read

import asyncio
from aiofile import AIOFile, Reader, Writer


async def main():
    async with AIOFile("/tmp/hello.txt", 'w+') as afp:
        await afp.write("Hello ")
        await afp.write("world", offset=7)
        await afp.fsync()

        print(await afp.read())


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Write and read with helpers

import asyncio
from aiofile import AIOFile, Reader, Writer


async def main():
    async with AIOFile("/tmp/hello.txt", 'w+') as afp:
        writer = Writer(afp)
        reader = Reader(afp, chunk_size=8)

        await writer("Hello")
        await writer(" ")
        await writer("World")
        await afp.fsync()

        async for chunk in reader:
            print(chunk)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Read file line by line

import asyncio
from aiofile import AIOFile, LineReader, Writer


async def main():
    async with AIOFile("/tmp/hello.txt", 'w+') as afp:
        writer = Writer(afp)

        await writer("Hello")
        await writer(" ")
        await writer("World")
        await writer("\n")
        await writer("\n")
        await writer("From async world")
        await afp.fsync()

        async for line in LineReader(afp):
            print(line)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Reading and Writing for the unix pipe

import os
import asyncio
from aiofile import AIOFile, Reader, Writer


async def reader(fname):
    print('Start reader')
    async with AIOFile(fname, 'r') as afp:
        while True:
            # Maximum expected chunk size, must be passed.
            # Otherwise will be read zero bytes
            # (because unix pipe has zero size)
            data = await afp.read(4096)
            print(data)


async def writer(fname):
    print('Start writer')
    async with AIOFile(fname, 'w') as afp:
        while True:
            await asyncio.sleep(1)
            await afp.write('%06f' % loop.time())


async def main():
    fifo_name = "/tmp/test.fifo"

    if os.path.exists(fifo_name):
        os.remove(fifo_name)

    os.mkfifo(fifo_name)

    # Starting two readers and one writer, but only one reader
    # will be reading at the same time.
    await asyncio.gather(
        reader(fifo_name),
        reader(fifo_name),
        writer(fifo_name),
    )


loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

try:
    loop.run_until_complete(main())
finally:
    # Shutting down and closing file descriptors after interrupt
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()
    print('Exited')

Read file line by line

import asyncio
from aiofile import AIOFile, LineReader, Writer


async def main():
    async with AIOFile("/tmp/hello.txt", 'w') as afp:
        writer = Writer(afp)

        for i in range(10):
            await writer("%d Hello World\n" % i)

        await writer("Tail-less string")


    async with AIOFile("/tmp/hello.txt", 'r') as afp:
        async for line in LineReader(afp):
            print(line[:-1])


loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Async CSV Dict Reader

import asyncio
import io
from csv import DictReader

from aiofile import AIOFile, LineReader


class AsyncDictReader:
    def __init__(self, afp, **kwargs):
        self.buffer = io.BytesIO()
        self.file_reader = LineReader(
            afp, line_sep=kwargs.pop('line_sep', '\n'),
            chunk_size=kwargs.pop('chunk_size', 4096),
            offset=kwargs.pop('offset', 0),
        )
        self.reader = DictReader(
            io.TextIOWrapper(
                self.buffer,
                encoding=kwargs.pop('encoding', 'utf-8'),
                errors=kwargs.pop('errors', 'replace'),
            ), **kwargs,
        )

    async def __aiter__(self):
        header = await self.file_reader.readline()

        if header:
            self.buffer.write(header)

        return self

    async def __anext__(self):
        line = await self.file_reader.readline()

        if not line:
            raise StopAsyncIteration

        self.buffer.write(line)
        self.buffer.seek(0)

        try:
            result = next(self.reader)
        except StopIteration as e:
            raise StopAsyncIteration from e

        self.buffer.truncate(0)

        return result


async def main():
    async with AIOFile('sample.csv', 'rb') as afp:
        async for item in AsyncDictReader(afp, line_sep='\r'):
            print(item)


loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)


try:
    loop.run_until_complete(main())
finally:
    # Shutting down and closing file descriptors after interrupt
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()

Metadata

Release files for aiofile 1.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiofile 1.4.3
File Size Uploaded Provenance Actions
aiofile-1.4.3.tar.gz 77.5 kB Without provenance Details for aiofile-1.4.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for aiofile 1.4.3
File Actions
aiofile-1.4.3-cp37-cp37m-manylinux1_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64 Details for aiofile-1.4.3-cp37-cp37m-manylinux1_x86_64.whl
aiofile-1.4.3-cp36-cp36m-manylinux1_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 Details for aiofile-1.4.3-cp36-cp36m-manylinux1_x86_64.whl
aiofile-1.4.3-cp36-cp36m-macosx_10_6_intel.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.6+ Intel (x86-64, i386) Details for aiofile-1.4.3-cp36-cp36m-macosx_10_6_intel.whl
aiofile-1.4.3-cp35-cp35m-manylinux1_x86_64.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64 Details for aiofile-1.4.3-cp35-cp35m-manylinux1_x86_64.whl
aiofile-1.4.3-cp35-cp35m-macosx_10_6_intel.whl CPython 3.5 CPython 3.5 pymalloc macOS 10.6+ Intel (x86-64, i386) Details for aiofile-1.4.3-cp35-cp35m-macosx_10_6_intel.whl
aiofile-1.4.3-cp34-cp34m-manylinux1_x86_64.whl CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64 Details for aiofile-1.4.3-cp34-cp34m-manylinux1_x86_64.whl
aiofile-1.4.3-cp34-cp34m-macosx_10_6_intel.whl CPython 3.4 CPython 3.4 pymalloc macOS 10.6+ Intel (x86-64, i386) Details for aiofile-1.4.3-cp34-cp34m-macosx_10_6_intel.whl

Total release size: 1.1 MB

Release files / aiofile-1.4.3.tar.gz

Download URL aiofile-1.4.3.tar.gz
Size 77.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d18d41842e9be7a63fa732de70acc6dde78defc688db5b1134e16136e419e26e
BLAKE2b-256 checksum
How to use checksums
38ec469c084dccd66216572289518ea1452bd07368f64b344165e0be97284278
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiofile-1.4.3-cp37-cp37m-manylinux1_x86_64.whl

Download URL aiofile-1.4.3-cp37-cp37m-manylinux1_x86_64.whl
Size 185.2 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
ef1dcb2900999d231c45fdd3626bedb1f2ec56b656a48c3ea6a854c3bcf9ebfa
BLAKE2b-256 checksum
How to use checksums
1e0b148bd5e7bbd47135d0458b4ab0193f9fa21440f0dec0f08e1e9935d8ed66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiofile-1.4.3-cp36-cp36m-manylinux1_x86_64.whl

Download URL aiofile-1.4.3-cp36-cp36m-manylinux1_x86_64.whl
Size 185.0 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
88d1d9a9a8f8eb45119faa98c1682a07628fa4cc1852a05e043b60770fcb9a9c
BLAKE2b-256 checksum
How to use checksums
6f401139e96c33e0b63809ac2df72999a0838f5380c7b518407a553cc397b3fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiofile-1.4.3-cp36-cp36m-macosx_10_6_intel.whl

Download URL aiofile-1.4.3-cp36-cp36m-macosx_10_6_intel.whl
Size 101.0 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
324007ad9fed27afd18840af7e2d1bd61f927ed58f7a47690b60190b4b5eec99
BLAKE2b-256 checksum
How to use checksums
8860991e6720ce416d7680767f0d1ffa78387318f5f8e2494028ee418df30ed7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiofile-1.4.3-cp35-cp35m-manylinux1_x86_64.whl

Download URL aiofile-1.4.3-cp35-cp35m-manylinux1_x86_64.whl
Size 185.7 kB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
93e5fa1b950036dd63e985de2367a779d73ed8370a18725db07c6e0e5152cba5
BLAKE2b-256 checksum
How to use checksums
183be6e9682a07c557ebc8c099012d2f547cceb72f066199b30f17d05b7d75e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiofile-1.4.3-cp35-cp35m-macosx_10_6_intel.whl

Download URL aiofile-1.4.3-cp35-cp35m-macosx_10_6_intel.whl
Size 100.2 kB
Tags CPython 3.5 CPython 3.5 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
aeb6ce34aac7c0b7af9014719f3a8bd84f5ab50a1c34a2598e9f1f805f988ebc
BLAKE2b-256 checksum
How to use checksums
047684c6dffc52161f19304cf9869e9637dae7ad91ca41078e08574e9a1e5c25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiofile-1.4.3-cp34-cp34m-manylinux1_x86_64.whl

Download URL aiofile-1.4.3-cp34-cp34m-manylinux1_x86_64.whl
Size 190.0 kB
Tags CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
c448780f52c88d313d7816421dc3dc3cc3fd6c4c9a459297e54bd08dc02ef1dd
BLAKE2b-256 checksum
How to use checksums
6eda4d11299f647101453a0da0db5c60c2b45faaa352a3fb3f4b18f7cf6cfc7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiofile-1.4.3-cp34-cp34m-macosx_10_6_intel.whl

Download URL aiofile-1.4.3-cp34-cp34m-macosx_10_6_intel.whl
Size 97.6 kB
Tags CPython 3.4 CPython 3.4 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
39ca4d11226eb4186ee0c4e8f0cb15dcb0d8f4a54b4ed94649227c485cfc7282
BLAKE2b-256 checksum
How to use checksums
a689894e9be4ad62fca6127a8e5e7ab2c662a9c880fe2a22ec3c55fd36e80772
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for aiofile 1.4.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.4.3 This release

8 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page