Python 的异步 pathlib
pathlib for Pythonaiopath is a complete implementation of Python's pathlib that's compatible with asyncio, trio, and the async/await syntax.
All I/O performed by aiopath is asynchronous and awaitable.
Check out app_paths for an example of library that uses aiopath, as well as the pyclean script here.
If you're writing asynchronous Python code and want to take advantage of pathlib's conveniences, but don't want to mix blocking and non-blocking I/O, then you can reach for aiopath.
For example, if you're writing an async web scraping script, you might want to make several concurrent requests to websites and save the responses to persistent storage:
…If you used pathlib instead of aiopath, tasks accessing the disk would block the event loop, and async tasks accessing the network would suspend until the event loop was unblocked.
By using aiopath, the script can access the network and disk concurrently.
aiopath is a direct reimplementation of CPython's pathlib.py and shares some of its code. aiopath's class hierarchy directly matches the one from pathlib, where Path inherits from PurePath, AsyncPath inherits from AsyncPurePath, and so on.
With aiopath, methods that perform I/O are asynchronous and awaitable, and methods that perform I/O and return iterators in pathlib now return async generators. aiopath goes one step further, and wraps os.scandir() and DirEntry to make AsyncPath.glob() completely async.
aiopath is typed with Python type annotations, and if using the aiofile back end, it takes advantage of libaio for async I/O on Linux.
aiopath's API directly matches pathlib, so check out the standard library documentation for PurePath and Path.
To run the following examples with top-level await expressions, launch an asynchronous Python REPL using python3 -m asyncio or an IPython shell.
You'll also need to install asynctempfile via PyPI, like so python3 -m pip install asynctempfile.
pathlibAll of pathlib.Path's methods that perform synchronous I/O are reimplemented as asynchronous methods. PurePath methods are not asynchronous because they don't perform I/O.
…You can convert pathlib.Path objects to aiopath.AsyncPath objects, and vice versa:
from pathlib import Path
from aiopath import AsyncPath
home: Path = Path.home()
ahome: AsyncPath = AsyncPath(home)
path: Path = Path(ahome)
assert isinstance(home, Path)
assert isinstance(ahome, AsyncPath)
assert isinstance(path, Path)
# AsyncPath and Path objects can point to the same file
assert str(home) == str(ahome) == str(path)
# AsyncPath and Path objects are equivalent
assert home == ahomeAsyncPath is a subclass of Path and PurePath, and a subclass of AsyncPurePath:
from pathlib import Path, PurePath
from aiopath import AsyncPath, AsyncPurePath
assert issubclass(AsyncPath, Path)
assert issubclass(AsyncPath, PurePath)
assert issubclass(AsyncPath, AsyncPurePath)
assert issubclass(AsyncPurePath, PurePath)
path: AsyncPath = await AsyncPath.home()
assert isinstance(path, Path)
assert isinstance(path, PurePath)
assert isinstance(path, AsyncPurePath) Check out the test files in the tests directory for more examples of how aiopath compares to pathlib.
You can get an asynchronous file-like object handle by using asynchronous context managers.
AsyncPath.open()'s async context manager yields an anyio.AsyncFile object.
…aiopath implements pathlib globbing using async I/O and async generators.
from aiopath import AsyncPath
home: AsyncPath = await AsyncPath.home()
async for path in home.glob('*'):
assert isinstance(path, AsyncPath)
print(path)
downloads: AsyncPath = home / 'Downloads'
if await downloads.exists():
# this might take a while
paths: list[AsyncPath] = \
[path async for path in downloads.glob('**/*')]requirements.txt$ python3 -m pip install aiopathaiopath for Python 3.9 and older is available on PyPI under versions 0.5.x and lower.
aiopath for Python 3.10 and newer is available on PyPI under versions 0.6.x and higher.
Download a release archive for your Python version from the releases page.
Then to build, run:
$ rye sync
$ rye buildA wheel will be compiled in dist/.
aiopath for Python 3.9 and older is developed on the Python-3.9 branch.
aiopath for Python 3.10 and newer is developed on the Python-3.10 branch.
Want to support this project and other open-source projects like it?
See LICENSE. If you'd like to use this project with a different license, please get in touch.
See CREDIT.md.
暂无开放 Issues,或尚未同步最近议题。