Asynchronous file operations.
Project description
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, 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')
Project details
Release history Release notifications | RSS feed
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.4.2.tar.gz
(76.6 kB
view details)
Built Distributions
File details
Details for the file aiofile-1.4.2.tar.gz
.
File metadata
- Download URL: aiofile-1.4.2.tar.gz
- Upload date:
- Size: 76.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd96a8615f55373d53b14fbd269fbc712cfc3c859ea1ae0b2a5ea47bda6a35f7 |
|
MD5 | b71dda4e05de2a7ef390b9b59969e56c |
|
BLAKE2b-256 | 017c9ebede7399adf80cd4169c21bf3c539a3bace0c850e983f5d8c2cb7ec692 |
File details
Details for the file aiofile-1.4.2-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: aiofile-1.4.2-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 184.5 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51f2ff00be78356a99b4daf64ece69382d28e775576324b6254847cdbc5a4e2c |
|
MD5 | 727f65b0b970cf5134dffa058be3b662 |
|
BLAKE2b-256 | 8abedbf758ff67215391280c11931974ede135a26330af309c22f9c40446708e |
File details
Details for the file aiofile-1.4.2-cp36-cp36m-macosx_10_6_intel.whl
.
File metadata
- Download URL: aiofile-1.4.2-cp36-cp36m-macosx_10_6_intel.whl
- Upload date:
- Size: 100.0 kB
- Tags: CPython 3.6m, macOS 10.6+ intel
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d20e29030172f72fd407a456090a9615f87cff6e7d916149d7244678246b46a |
|
MD5 | 119ee0f378419c2b84cd3393b39b9875 |
|
BLAKE2b-256 | 684dcb2bd710ce73eacdf61a8cc65501df0b3b45b7e7c6b2f5f347da2e4345b1 |
File details
Details for the file aiofile-1.4.2-cp35-cp35m-manylinux1_x86_64.whl
.
File metadata
- Download URL: aiofile-1.4.2-cp35-cp35m-manylinux1_x86_64.whl
- Upload date:
- Size: 185.2 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3efa8ae12f8649e173b025ea8f0eb85c1ed4746e8b4379126617e897e469456 |
|
MD5 | 7d9a0295421ff8798a04ac4b563d11b3 |
|
BLAKE2b-256 | 53e07487cd4586af0bd55b78f6667396a86da8b92cacc492a0fb0958ed7ccca0 |
File details
Details for the file aiofile-1.4.2-cp35-cp35m-macosx_10_6_intel.whl
.
File metadata
- Download URL: aiofile-1.4.2-cp35-cp35m-macosx_10_6_intel.whl
- Upload date:
- Size: 99.2 kB
- Tags: CPython 3.5m, macOS 10.6+ intel
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1bea098107215418d2ffd1688bf6e01de6c6406851f11a1c4b6b94077c24481 |
|
MD5 | aa8b0eedc325e56ee5d6fec00ee5dcb4 |
|
BLAKE2b-256 | 76c185fe63f85d165ef62fd0cc8935700557f1a198a59980d90b3b2ad42e8806 |
File details
Details for the file aiofile-1.4.2-cp34-cp34m-manylinux1_x86_64.whl
.
File metadata
- Download URL: aiofile-1.4.2-cp34-cp34m-manylinux1_x86_64.whl
- Upload date:
- Size: 189.5 kB
- Tags: CPython 3.4m
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 821b99e79114ce7d42ff06d65dcc41652958a7135e57d3e5823fd94ade862d6a |
|
MD5 | 3ab4116fab6f56cbc5b0d1d3ab4463fe |
|
BLAKE2b-256 | f93abe79caf807a9a35925efa9248678b9d12c01d1db7f70c856c16a094232f2 |
File details
Details for the file aiofile-1.4.2-cp34-cp34m-macosx_10_6_intel.whl
.
File metadata
- Download URL: aiofile-1.4.2-cp34-cp34m-macosx_10_6_intel.whl
- Upload date:
- Size: 97.1 kB
- Tags: CPython 3.4m, macOS 10.6+ intel
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2f8b44aed7d742d387c915f1b9d93e71639061c1cb3bc35ddce9e4c267cec84 |
|
MD5 | ddec11be6aa6b2115a8c41bc77720198 |
|
BLAKE2b-256 | db8641e96f339f81c013af1114b4ed2d9966daf7f78f65c35ed83cb092d6b147 |