AIOFile
Real asynchronous file operations with asyncio support.
Status
Development - Stable
Features
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()
Metadata
Release files for aiofile 1.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aiofile-1.5.2.tar.gz | 83.1 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| aiofile-1.5.2-cp37-cp37m-manylinux1_x86_64.whl | CPython 3.7 | CPython 3.7 pymalloc | Linux glibc 2.5+ x86-64 | Details |
| aiofile-1.5.2-cp36-cp36m-manylinux1_x86_64.whl | CPython 3.6 | CPython 3.6 pymalloc | Linux glibc 2.5+ x86-64 | Details |
| aiofile-1.5.2-cp36-cp36m-macosx_10_9_x86_64.whl | CPython 3.6 | CPython 3.6 pymalloc | macOS 10.9+ x86-64 | Details |
| aiofile-1.5.2-cp35-cp35m-manylinux1_x86_64.whl | CPython 3.5 | CPython 3.5 pymalloc | Linux glibc 2.5+ x86-64 | Details |
| aiofile-1.5.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.5.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: 1.0 MB
Release files / aiofile-1.5.2.tar.gz
| Download URL | aiofile-1.5.2.tar.gz |
|---|---|
| Size | 83.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
229078abbaab87adfcaad0fa7766b9b8251d42d0242deac6166da433b027ef1f
|
|
BLAKE2b-256 checksum How to use checksums |
717cd6df3b2c8fb380ef7f678c366dc4485dfeb389967d373b1dc5d53bd1dcf0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / aiofile-1.5.2-cp37-cp37m-manylinux1_x86_64.whl
| Download URL | aiofile-1.5.2-cp37-cp37m-manylinux1_x86_64.whl |
|---|---|
| Size | 208.9 kB |
| Tags | CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
312d50ed7e646a40ab2a5457fdf382870aca926f956921ab8c7ab72c3922f372
|
|
BLAKE2b-256 checksum How to use checksums |
a8fdb46f10a35a0a606e2d2c0d0e2bb7e28d4dd2b4f549b5d695540c7920895a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / aiofile-1.5.2-cp36-cp36m-manylinux1_x86_64.whl
| Download URL | aiofile-1.5.2-cp36-cp36m-manylinux1_x86_64.whl |
|---|---|
| Size | 210.1 kB |
| Tags | CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
d1da2fc9aa7509d29ea09617bf533bd1045f79cfdfb10ee83da90ba2212720a2
|
|
BLAKE2b-256 checksum How to use checksums |
1d6278f57d27f77b50cd926130469758c350896d9d136b1cee1f8702903b7f8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / aiofile-1.5.2-cp36-cp36m-macosx_10_9_x86_64.whl
| Download URL | aiofile-1.5.2-cp36-cp36m-macosx_10_9_x86_64.whl |
|---|---|
| Size | 70.5 kB |
| Tags | CPython 3.6 CPython 3.6 pymalloc macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
8c50fcb42ee2bad2ae811edb972724e7f6bf3b0a6565a498f2432862b548b92d
|
|
BLAKE2b-256 checksum How to use checksums |
3f85112ce2cc882b523c28d346d709d400fce0b1b66a26998bd35a5ad107ebac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / aiofile-1.5.2-cp35-cp35m-manylinux1_x86_64.whl
| Download URL | aiofile-1.5.2-cp35-cp35m-manylinux1_x86_64.whl |
|---|---|
| Size | 205.6 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
a9a457654e561c396b88f70a0d5fa00e40a337853aa180bc805d9d5efb82317c
|
|
BLAKE2b-256 checksum How to use checksums |
d7b6aeb1184e470d57e6f7f284b8f4024b01ab34770348288066e72ee9f6d135
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / aiofile-1.5.2-cp35-cp35m-macosx_10_6_intel.whl
| Download URL | aiofile-1.5.2-cp35-cp35m-macosx_10_6_intel.whl |
|---|---|
| Size | 116.9 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc macOS 10.6+ Intel (x86-64, i386) |
|
SHA-256 checksum How to use checksums |
e43cb5e3181a8dfb73afbb4749b024e9a35a52e60ecf97d1d3db2731212cb0a0
|
|
BLAKE2b-256 checksum How to use checksums |
27efb3f66346fac1b3eea69f3dd376ec2596b8a5948bc66132550ab1c6aebc7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / aiofile-1.5.2-cp34-cp34m-macosx_10_6_intel.whl
| Download URL | aiofile-1.5.2-cp34-cp34m-macosx_10_6_intel.whl |
|---|---|
| Size | 114.9 kB |
| Tags | CPython 3.4 CPython 3.4 pymalloc macOS 10.6+ Intel (x86-64, i386) |
|
SHA-256 checksum How to use checksums |
cef9e7bdf93db6a4c7ffe9ef0c354e2887695ec2a3a9dda8ed285005ec835616
|
|
BLAKE2b-256 checksum How to use checksums |
990717cc18326999e1fa84caa8ff147723a69230ee13173cd213717ada3f3325
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|