Skip to main content

this will be removed after the origin aiofiles38 supports new versions of Python

Project description

https://img.shields.io/pypi/v/aiofiles.svg https://travis-ci.org/Tinche/aiofiles.svg?branch=master https://codecov.io/gh/Tinche/aiofiles/branch/master/graph/badge.svg Supported Python versions

aiofiles is an Apache2 licensed library, written in Python, for handling local disk files in asyncio applications.

Ordinary local file IO is blocking, and cannot easily and portably made asynchronous. This means doing file IO may interfere with asyncio applications, which shouldn’t block the executing thread. aiofiles helps with this by introducing asynchronous versions of files that support delegating operations to a separate thread pool.

async with aiofiles.open('filename', mode='r') as f:
    contents = await f.read()
print(contents)
'My file contents'

Asynchronous iteration is also supported.

async with aiofiles.open('filename') as f:
    async for line in f:
        ...

Features

  • a file API very similar to Python’s standard, blocking API

  • support for buffered and unbuffered binary files, and buffered text files

  • support for async/await (PEP 492) constructs

Installation

To install aiofiles, simply:

$ pip install aiofiles

Usage

Files are opened using the aiofiles.open() coroutine, which in addition to mirroring the builtin open accepts optional loop and executor arguments. If loop is absent, the default loop will be used, as per the set asyncio policy. If executor is not specified, the default event loop executor will be used.

In case of success, an asynchronous file object is returned with an API identical to an ordinary file, except the following methods are coroutines and delegate to an executor:

  • close

  • flush

  • isatty

  • read

  • readall

  • read1

  • readinto

  • readline

  • readlines

  • seek

  • seekable

  • tell

  • truncate

  • writable

  • write

  • writelines

In case of failure, one of the usual exceptions will be raised.

The aiofiles.os module contains executor-enabled coroutine versions of several useful os functions that deal with files:

  • stat

  • sendfile

  • rename

  • remove

  • mkdir

  • rmdir

Writing tests for aiofiles

Real file IO can be mocked by patching aiofiles.threadpool.sync_open as desired. The return type also needs to be registered with the aiofiles.threadpool.wrap dispatcher:

aiofiles.threadpool.wrap.register(mock.MagicMock)(
    lambda *args, **kwargs: threadpool.AsyncBufferedIOBase(*args, **kwargs))

async def test_stuff():
    data = 'data'
    mock_file = mock.MagicMock()

    with mock.patch('aiofiles.threadpool.sync_open', return_value=mock_file) as mock_open:
        async with aiofiles.open('filename', 'w') as f:
            await f.write(data)

        mock_file.write.assert_called_once_with(data)

History

0.4.0 (2018-08-11)

  • Python 3.7 support.

  • Removed Python 3.3/3.4 support. If you use these versions, stick to aiofiles 0.3.x.

0.3.2 (2017-09-23)

  • The LICENSE is now included in the sdist. #31

0.3.1 (2017-03-10)

  • Introduced a changelog.

  • aiofiles.os.sendfile will now work if the standard os module contains a sendfile function.

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

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

aiofiles38-0.5.0.dev0.tar.gz (9.3 kB view details)

Uploaded Source

File details

Details for the file aiofiles38-0.5.0.dev0.tar.gz.

File metadata

  • Download URL: aiofiles38-0.5.0.dev0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.8

File hashes

Hashes for aiofiles38-0.5.0.dev0.tar.gz
Algorithm Hash digest
SHA256 f05fdfa125364521897cbbf43b0974b3936e2c27496c6a7b25aa15d7a1e4f1b5
MD5 eb305b24ec8084676c13f16be9bb5029
BLAKE2b-256 5cea10d6892392519edc817eca63361ea2787f155f003701a48e95c2bf5d29b8

See more details on using hashes here.

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