Skip to main content

Async Files

Project description

async-files

async-files is a fast, lightweight, and extensible asyncio file library, written in pure python and inspired by aiofiles. This works just like aiofiles which delegates file IO operations to a separate thread pool. Although, async-files have been completely re-written from scratch to use modern async...await... syntax and with extensibility and flexibility in mind.

Usage

Files can be opened with async context manager or by calling FileIO instance.

async with FileIO("README.md") as f:
    s = await f.read()
    print(s)

or

f = await FileIO("README.md")()  # __init__ can't be asynchronous.
s = await f.read()
print(s)
await f.close()

Asynchronous iteration is also supported.

async with FileIO("README.md") as f:
    async for line in f:
        print(line, end="")

You can also extend functionality of FileIO to support other classes like tempfile.TemporaryFile very easily:

from tempfile import TemporaryFile as _TemporaryFile
from async_files import FileIO

class TemporaryFile(FileIO):
    OPEN = _TemporaryFile

You can do same for any IO classes like GzipFile, SpooledTemporaryFile, etc.

Following are asynchronous attributes of the FileIO object.

close: Callable[[], Awaitable[None]]
flush: Callable[[], Awaitable[None]]
isatty: Callable[[], Awaitable[bool]]
read: Callable[[], Awaitable[Union[str, bytes]]]
read1: Callable[[], Awaitable[bytes]]
readinto: Callable[[bytearray], Awaitable[int]]
readinto1: Callable[[bytearray], Awaitable[int]]
readline: Callable[[], Awaitable[Union[str, bytes]]]
readlines: Callable[[], Awaitable[List[Union[str, bytes]]]]
seek: Callable[[], Awaitable[int]]
tell: Callable[[], Awaitable[int]]
truncate: Callable[[], Awaitable[int]]
write: Callable[[Union[str, bytes]], Awaitable[int]]
writelines: Callable[[List[Union[str, bytes]]], Awaitable[None]]

Other attributes are synchronous just like standard library fileobj.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

async_files-0.2-py2.py3-none-any.whl (7.0 kB view hashes)

Uploaded Python 2 Python 3

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