Skip to main content
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')

Metadata

Release files for aiofile 1.4.2

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.2
File Size Uploaded
aiofile-1.4.2.tar.gz 76.6 kB Details

Built distributions (wheels)

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

Total release size: 932.2 kB

Release files / aiofile-1.4.2.tar.gz

Download URL aiofile-1.4.2.tar.gz
Size 76.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bd96a8615f55373d53b14fbd269fbc712cfc3c859ea1ae0b2a5ea47bda6a35f7
BLAKE2b-256 checksum
How to use checksums
017c9ebede7399adf80cd4169c21bf3c539a3bace0c850e983f5d8c2cb7ec692
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL aiofile-1.4.2-cp36-cp36m-manylinux1_x86_64.whl
Size 184.5 kB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
51f2ff00be78356a99b4daf64ece69382d28e775576324b6254847cdbc5a4e2c
BLAKE2b-256 checksum
How to use checksums
8abedbf758ff67215391280c11931974ede135a26330af309c22f9c40446708e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL aiofile-1.4.2-cp36-cp36m-macosx_10_6_intel.whl
Size 100.0 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
9d20e29030172f72fd407a456090a9615f87cff6e7d916149d7244678246b46a
BLAKE2b-256 checksum
How to use checksums
684dcb2bd710ce73eacdf61a8cc65501df0b3b45b7e7c6b2f5f347da2e4345b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL aiofile-1.4.2-cp35-cp35m-manylinux1_x86_64.whl
Size 185.2 kB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
a3efa8ae12f8649e173b025ea8f0eb85c1ed4746e8b4379126617e897e469456
BLAKE2b-256 checksum
How to use checksums
53e07487cd4586af0bd55b78f6667396a86da8b92cacc492a0fb0958ed7ccca0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

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

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

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

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

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

Release history Release notifications | RSS feed

3.11.1

2 release files

3.11.0

2 release files

3.10.2

2 release files

3.10.1

2 release files

3.10.0

2 release files

3.9.0

2 release files

3.8.8

2 release files

3.8.7

2 release files

3.8.6

2 release files

3.8.5

2 release files

3.8.4

2 release files

3.8.3

2 release files

3.8.2

1 release file

3.8.1

1 release file

3.8.0

1 release file

3.7.4

2 release files

3.7.3

2 release files

3.7.2

2 release files

3.7.1

2 release files

3.7.0

2 release files

3.6.0

2 release files

3.5.1

1 release file

3.5.0

2 release files

3.4.0

2 release files

3.3.3

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5.2

7 release files

1.5.1

8 release files

1.4.3

8 release files

This release

1.4.2 This release

7 release files

1.4.0

8 release files

1.3.0

7 release files

1.2.6

7 release files

1.2.5

7 release files

1.2.2

7 release files

1.2.1

7 release files

1.2.0

7 release files

0.5.3

7 release files

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