You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

139 line
3.8 KiB

  1. aiofiles: file support for asyncio
  2. ==================================
  3. .. image:: https://img.shields.io/pypi/v/aiofiles.svg
  4. :target: https://pypi.python.org/pypi/aiofiles
  5. .. image:: https://travis-ci.org/Tinche/aiofiles.svg?branch=master
  6. :target: https://travis-ci.org/Tinche/aiofiles
  7. .. image:: https://codecov.io/gh/Tinche/aiofiles/branch/master/graph/badge.svg
  8. :target: https://codecov.io/gh/Tinche/aiofiles
  9. **aiofiles** is an Apache2 licensed library, written in Python, for handling local
  10. disk files in asyncio applications.
  11. Ordinary local file IO is blocking, and cannot easily and portably made
  12. asynchronous. This means doing file IO may interfere with asyncio applications,
  13. which shouldn't block the executing thread. aiofiles helps with this by
  14. introducing asynchronous versions of files that support delegating operations to
  15. a separate thread pool.
  16. .. code-block:: python
  17. async with aiofiles.open('filename', mode='r') as f:
  18. contents = await f.read()
  19. print(contents)
  20. 'My file contents'
  21. Asynchronous iteration is also supported.
  22. .. code-block:: python
  23. async with aiofiles.open('filename') as f:
  24. async for line in f:
  25. ...
  26. Features
  27. --------
  28. - a file API very similar to Python's standard, blocking API
  29. - support for buffered and unbuffered binary files, and buffered text files
  30. - support for ``async``/``await`` (:PEP:`492`) constructs
  31. Installation
  32. ------------
  33. To install aiofiles, simply:
  34. .. code-block:: bash
  35. $ pip install aiofiles
  36. Usage
  37. -----
  38. Files are opened using the ``aiofiles.open()`` coroutine, which in addition to
  39. mirroring the builtin ``open`` accepts optional ``loop`` and ``executor``
  40. arguments. If ``loop`` is absent, the default loop will be used, as per the
  41. set asyncio policy. If ``executor`` is not specified, the default event loop
  42. executor will be used.
  43. In case of success, an asynchronous file object is returned with an
  44. API identical to an ordinary file, except the following methods are coroutines
  45. and delegate to an executor:
  46. * ``close``
  47. * ``flush``
  48. * ``isatty``
  49. * ``read``
  50. * ``readall``
  51. * ``read1``
  52. * ``readinto``
  53. * ``readline``
  54. * ``readlines``
  55. * ``seek``
  56. * ``seekable``
  57. * ``tell``
  58. * ``truncate``
  59. * ``writable``
  60. * ``write``
  61. * ``writelines``
  62. In case of failure, one of the usual exceptions will be raised.
  63. The ``aiofiles.os`` module contains executor-enabled coroutine versions of
  64. several useful ``os`` functions that deal with files:
  65. * ``stat``
  66. * ``sendfile``
  67. Writing tests for aiofiles
  68. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. Real file IO can be mocked by patching ``aiofiles.threadpool.sync_open``
  70. as desired. The return type also needs to be registered with the
  71. ``aiofiles.threadpool.wrap`` dispatcher:
  72. .. code-block:: python
  73. aiofiles.threadpool.wrap.register(mock.MagicMock)(
  74. lambda *args, **kwargs: threadpool.AsyncBufferedIOBase(*args, **kwargs))
  75. async def test_stuff():
  76. data = 'data'
  77. mock_file = mock.MagicMock()
  78. with mock.patch('aiofiles.threadpool.sync_open', return_value=mock_file) as mock_open:
  79. async with aiofiles.open('filename', 'w') as f:
  80. await f.write(data)
  81. mock_file.write.assert_called_once_with(data)
  82. History
  83. ~~~~~~~
  84. 0.4.0 (2018-08-11)
  85. ``````````````````
  86. - Python 3.7 support.
  87. - Removed Python 3.3/3.4 support. If you use these versions, stick to aiofiles 0.3.x.
  88. 0.3.2 (2017-09-23)
  89. ``````````````````
  90. - The LICENSE is now included in the sdist.
  91. `#31 <https://github.com/Tinche/aiofiles/pull/31>`_
  92. 0.3.1 (2017-03-10)
  93. ``````````````````
  94. - Introduced a changelog.
  95. - ``aiofiles.os.sendfile`` will now work if the standard ``os`` module contains a ``sendfile`` function.
  96. Contributing
  97. ~~~~~~~~~~~~
  98. Contributions are very welcome. Tests can be run with ``tox``, please ensure
  99. the coverage at least stays the same before you submit a pull request.