Python asyncio 是否支持文件操作的异步 I/O?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34699948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does asyncio supports asynchronous I/O for file operations?
提问by CthUlhUzzz
Does asyncio supports asynchronous I/O for file operations? If yes, how I can use this in Python 3.5 with async/await syntax code?
asyncio 是否支持文件操作的异步 I/O?如果是,我如何在带有异步/等待语法代码的 Python 3.5 中使用它?
回答by Andrew Svetlov
Most operating systems don't support asynchronous file operations.
That's why asyncio
doesn't support them either.
大多数操作系统不支持异步文件操作。这也是为什么asyncio
不支持他们的原因。
See the asyncio wikifor further explanation.
请参阅asyncio wiki以获取进一步说明。
回答by Dima Tisnek
That depends what library you use.
那要看你用什么库了。
curio
offers this functionality, https://curio.readthedocs.io/en/latest/reference.html#module-curio.file
curio
提供此功能,https://curio.readthedocs.io/en/latest/reference.html#module-curio.file
plain asyncio
doesn't, but there are 3rd party libraries, e.g. https://github.com/Tinche/aiofiles(which is really synchronous file access isolated in threads)
普通asyncio
没有,但有 3rd 方库,例如https://github.com/Tinche/aiofiles(这是真正的线程中隔离的同步文件访问)
Modern operating systems do provide asynchronous file primitives, but these are varied, thus each would need own implementation. Please compare:
现代操作系统确实提供了异步文件原语,但这些原语各不相同,因此每个都需要自己的实现。请比较:
- http://man7.org/linux/man-pages/man7/aio.7.html
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa365683(v=vs.85).aspx
- https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/TechniquesforReadingandWritingCustomFiles/TechniquesforReadingandWritingCustomFiles.html
- http://man7.org/linux/man-pages/man7/aio.7.html
- https://msdn.microsoft.com/en-us/library/windows/desktop/aa365683(v=vs.85).aspx
- https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/TechniquesforReadingandWritingCustomFiles/TechniquesforReadingandWritingCustomFiles.html
I suspect someone will soon rip out underlying async io from node.js
and make a decent Python library, or perhaps someone already has.
我怀疑有人很快就会从底层的异步 io 中剥离出来node.js
并制作一个像样的 Python 库,或者也许有人已经拥有了。