Skip to main content

Asynchronous file operations.

Project description

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.flush()

        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.flush()

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


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

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.3.0.tar.gz (75.6 kB view hashes)

Uploaded Source

Built Distributions

aiofile-1.3.0-cp36-cp36m-manylinux1_x86_64.whl (184.2 kB view hashes)

Uploaded CPython 3.6m

aiofile-1.3.0-cp36-cp36m-macosx_10_6_intel.whl (98.2 kB view hashes)

Uploaded CPython 3.6m macOS 10.6+ intel

aiofile-1.3.0-cp35-cp35m-manylinux1_x86_64.whl (184.8 kB view hashes)

Uploaded CPython 3.5m

aiofile-1.3.0-cp35-cp35m-macosx_10_6_intel.whl (97.0 kB view hashes)

Uploaded CPython 3.5m macOS 10.6+ intel

aiofile-1.3.0-cp34-cp34m-manylinux1_x86_64.whl (190.0 kB view hashes)

Uploaded CPython 3.4m

aiofile-1.3.0-cp34-cp34m-macosx_10_6_intel.whl (97.3 kB view hashes)

Uploaded CPython 3.4m macOS 10.6+ intel

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