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

Totally async read and write:

import asyncio
from aiofile import AIOFile, Reader, Writer

async def main(loop):
    aio_file = await AIOFile("/tmp/hello.txt", 'w+', loop=loop)

    await aio_file.write(b"Hello ")
    await aio_file.write(b"world", offset=7)
    await aio_file.fsync()

    print(await aio_file.read())

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

Write and read with helpers:

import asyncio
from aiofile import AIOFile, Reader, Writer

async def main(loop):
    aio_file = await AIOFile("/tmp/hello.txt", 'w+', loop=loop)

    writer = Writer(aio_file)
    reader = Reader(aio_file, chunk_size=8)

    await writer(b"Hello")
    await writer(b" ")
    await writer(b"World")
    await aio_file.flush()

    async for chunk in reader:
        print(chunk)

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

or use async context manager:

import asyncio
from aiofile import AIOFile, Reader, Writer

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

        await writer(b"Hello")
        await writer(b" ")
        await writer(b"World")
        await aio_file.flush()

        async for chunk in reader:
            print(chunk)

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

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

Uploaded Source

Built Distributions

aiofile-1.2.0-cp36-cp36m-manylinux1_x86_64.whl (183.5 kB view hashes)

Uploaded CPython 3.6m

aiofile-1.2.0-cp36-cp36m-macosx_10_6_intel.whl (97.6 kB view hashes)

Uploaded CPython 3.6m macOS 10.6+ intel

aiofile-1.2.0-cp35-cp35m-manylinux1_x86_64.whl (184.2 kB view hashes)

Uploaded CPython 3.5m

aiofile-1.2.0-cp35-cp35m-macosx_10_6_intel.whl (96.4 kB view hashes)

Uploaded CPython 3.5m macOS 10.6+ intel

aiofile-1.2.0-cp34-cp34m-manylinux1_x86_64.whl (189.4 kB view hashes)

Uploaded CPython 3.4m

aiofile-1.2.0-cp34-cp34m-macosx_10_6_intel.whl (96.7 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