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,
        )
        self.line_num = 0

    def __aiter__(self):
        return self

    async def __anext__(self):
        if self.line_num == 0:
            header = await self.file_reader.readline()
            self.buffer.write(header)

        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.seek(0)
        self.buffer.truncate(0)
        self.line_num = self.reader.line_num

        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.2.tar.gz (83.1 kB view details)

Uploaded Source

Built Distributions

aiofile-1.5.2-cp37-cp37m-manylinux1_x86_64.whl (208.9 kB view details)

Uploaded CPython 3.7m

aiofile-1.5.2-cp36-cp36m-manylinux1_x86_64.whl (210.1 kB view details)

Uploaded CPython 3.6m

aiofile-1.5.2-cp36-cp36m-macosx_10_9_x86_64.whl (70.5 kB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

aiofile-1.5.2-cp35-cp35m-manylinux1_x86_64.whl (205.6 kB view details)

Uploaded CPython 3.5m

aiofile-1.5.2-cp35-cp35m-macosx_10_6_intel.whl (116.9 kB view details)

Uploaded CPython 3.5m macOS 10.6+ intel

aiofile-1.5.2-cp34-cp34m-macosx_10_6_intel.whl (114.9 kB view details)

Uploaded CPython 3.4m macOS 10.6+ intel

File details

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

File metadata

  • Download URL: aiofile-1.5.2.tar.gz
  • Upload date:
  • Size: 83.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiofile-1.5.2.tar.gz
Algorithm Hash digest
SHA256 229078abbaab87adfcaad0fa7766b9b8251d42d0242deac6166da433b027ef1f
MD5 190e2b1a61df3b395787784e2b1729f0
BLAKE2b-256 717cd6df3b2c8fb380ef7f678c366dc4485dfeb389967d373b1dc5d53bd1dcf0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiofile-1.5.2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 208.9 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiofile-1.5.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 312d50ed7e646a40ab2a5457fdf382870aca926f956921ab8c7ab72c3922f372
MD5 6d44258b22329072fba3f84568570bc1
BLAKE2b-256 a8fdb46f10a35a0a606e2d2c0d0e2bb7e28d4dd2b4f549b5d695540c7920895a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiofile-1.5.2-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 210.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiofile-1.5.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d1da2fc9aa7509d29ea09617bf533bd1045f79cfdfb10ee83da90ba2212720a2
MD5 7419c1ad5eca2e0df5ec1eb56735fee0
BLAKE2b-256 1d6278f57d27f77b50cd926130469758c350896d9d136b1cee1f8702903b7f8b

See more details on using hashes here.

File details

Details for the file aiofile-1.5.2-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: aiofile-1.5.2-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 70.5 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiofile-1.5.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8c50fcb42ee2bad2ae811edb972724e7f6bf3b0a6565a498f2432862b548b92d
MD5 b417c22d290daba03fadb3ceaffb6aff
BLAKE2b-256 3f85112ce2cc882b523c28d346d709d400fce0b1b66a26998bd35a5ad107ebac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiofile-1.5.2-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 205.6 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiofile-1.5.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a9a457654e561c396b88f70a0d5fa00e40a337853aa180bc805d9d5efb82317c
MD5 3005a23d9e13603edd9010d16ab3c7e6
BLAKE2b-256 d7b6aeb1184e470d57e6f7f284b8f4024b01ab34770348288066e72ee9f6d135

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiofile-1.5.2-cp35-cp35m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 116.9 kB
  • Tags: CPython 3.5m, macOS 10.6+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiofile-1.5.2-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 e43cb5e3181a8dfb73afbb4749b024e9a35a52e60ecf97d1d3db2731212cb0a0
MD5 1b73f6aef91cf534e0a1fce02fe6bc42
BLAKE2b-256 27efb3f66346fac1b3eea69f3dd376ec2596b8a5948bc66132550ab1c6aebc7f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiofile-1.5.2-cp34-cp34m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 114.9 kB
  • Tags: CPython 3.4m, macOS 10.6+ intel
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for aiofile-1.5.2-cp34-cp34m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 cef9e7bdf93db6a4c7ffe9ef0c354e2887695ec2a3a9dda8ed285005ec835616
MD5 10c0440458e0295096016f2aaaee5129
BLAKE2b-256 990717cc18326999e1fa84caa8ff147723a69230ee13173cd213717ada3f3325

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