Skip to main content

Asynchronous file operations.

Project description

Drone 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


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, 'a') 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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiofile-1.5.1.tar.gz (83.0 kB view details)

Uploaded Source

Built Distributions

aiofile-1.5.1-cp37-cp37m-manylinux1_x86_64.whl (206.9 kB view details)

Uploaded CPython 3.7m

aiofile-1.5.1-cp36-cp36m-manylinux1_x86_64.whl (209.4 kB view details)

Uploaded CPython 3.6m

aiofile-1.5.1-cp36-cp36m-macosx_10_6_intel.whl (115.8 kB view details)

Uploaded CPython 3.6m macOS 10.6+ intel

aiofile-1.5.1-cp35-cp35m-manylinux1_x86_64.whl (206.0 kB view details)

Uploaded CPython 3.5m

aiofile-1.5.1-cp35-cp35m-macosx_10_6_intel.whl (110.2 kB view details)

Uploaded CPython 3.5m macOS 10.6+ intel

aiofile-1.5.1-cp34-cp34m-manylinux1_x86_64.whl (204.0 kB view details)

Uploaded CPython 3.4m

aiofile-1.5.1-cp34-cp34m-macosx_10_6_intel.whl (110.4 kB view details)

Uploaded CPython 3.4m macOS 10.6+ intel

File details

Details for the file aiofile-1.5.1.tar.gz.

File metadata

  • Download URL: aiofile-1.5.1.tar.gz
  • Upload date:
  • Size: 83.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1.tar.gz
Algorithm Hash digest
SHA256 bb4360c48e613271dc6f313aa26d0ea939e3e4009b828273e66d3a4aa2445ca1
MD5 4a0b8db33de3eb34c000a38c0d02fdb3
BLAKE2b-256 37a38707b3df927239f8e1e9afe933be0c5ffd4444a620af9bf9e4fe5db8684d

See more details on using hashes here.

File details

Details for the file aiofile-1.5.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: aiofile-1.5.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 206.9 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 09b9c7e5b385055bb1f114a993959e03f05113ff9b7e590b2ad3b686084c9336
MD5 20355b1682c6b7f8dceb0b145b2bbb26
BLAKE2b-256 e03df7c8a0f3614d13d6c67f517fab5a652378ce1eece7001e154aa97ad54b7e

See more details on using hashes here.

File details

Details for the file aiofile-1.5.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: aiofile-1.5.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 209.4 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1344addebde13d13f5255aa9254275e9b22ca81dee7e27cd02f871a7e31f96e3
MD5 722b6bf4570331af6d7512fac4185ead
BLAKE2b-256 e5a1df54a4a77efd8441198f26cf6bb91cee90c5d33375f1430a2896bd83d1d2

See more details on using hashes here.

File details

Details for the file aiofile-1.5.1-cp36-cp36m-macosx_10_6_intel.whl.

File metadata

  • Download URL: aiofile-1.5.1-cp36-cp36m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 115.8 kB
  • Tags: CPython 3.6m, macOS 10.6+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 13608ce26fc51c76679741f590151db61970dbf5623378033515b1298383a5b3
MD5 b14a2684c5266f20833e58e72aa6dd38
BLAKE2b-256 183f8358d4a526f1ca592bad7d6b51b1667aac6bf9063bf9fff082f1ea1b37dd

See more details on using hashes here.

File details

Details for the file aiofile-1.5.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: aiofile-1.5.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 206.0 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 08f867f111d10d056199191e514e2e2973c0f8dba58ca75f27627aff1e657e51
MD5 013d5b796c544a3a54738f9c462d4595
BLAKE2b-256 8aa2a6a32809dabe727300eb10558b5519d4733eb7d5471c48ffa50751f453fc

See more details on using hashes here.

File details

Details for the file aiofile-1.5.1-cp35-cp35m-macosx_10_6_intel.whl.

File metadata

  • Download URL: aiofile-1.5.1-cp35-cp35m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 110.2 kB
  • Tags: CPython 3.5m, macOS 10.6+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 f60267e2713e201716df7b386baba88b5e619e1b9ae8bf2b938aec05d2fac999
MD5 7d8350ec572f6e80c7c8b01b1b0a79cb
BLAKE2b-256 da412ecf3723b785f7210f9c3916bd00850a31cfe752114969071acf4bd499a2

See more details on using hashes here.

File details

Details for the file aiofile-1.5.1-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

  • Download URL: aiofile-1.5.1-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 204.0 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e9eb5a777dbf3c0694fc40af09a2084afd1d78179b83e65faeae40a328e92795
MD5 58753c9921afa30b26cf903557982bec
BLAKE2b-256 67326695d66d39ceedf09d9b9f2ab00bac5f0beba452fdf11869925f2911514f

See more details on using hashes here.

File details

Details for the file aiofile-1.5.1-cp34-cp34m-macosx_10_6_intel.whl.

File metadata

  • Download URL: aiofile-1.5.1-cp34-cp34m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 110.4 kB
  • Tags: CPython 3.4m, macOS 10.6+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/33.1.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

File hashes

Hashes for aiofile-1.5.1-cp34-cp34m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 eac48b01a0c06245f955f90cc7e299355adcc319524cb811572be2f4acb4254b
MD5 3a483ac62098ce18b0c68032aed26b80
BLAKE2b-256 3b3e74fdbbb778f7236175ece0663457c756fd0b53d0ddd760916f044c69f8db

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page