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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:23:24  来源:igfitidea点击:

Does asyncio supports asynchronous I/O for file operations?

pythonpython-3.xpython-asynciopython-3.5

提问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 asynciodoesn't support them either.

大多数操作系统不支持异步文件操作。这也是为什么asyncio不支持他们的原因。

See the asyncio wikifor further explanation.

请参阅asyncio wiki以获取进一步说明。

回答by Dima Tisnek

That depends what library you use.

那要看你用什么库了。

curiooffers 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 asynciodoesn'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:

现代操作系统确实提供了异步文件原语,但这些原语各不相同,因此每个都需要自己的实现。请比较:

I suspect someone will soon rip out underlying async io from node.jsand make a decent Python library, or perhaps someone already has.

我怀疑有人很快就会从底层的异步 io 中剥离出来node.js并制作一个像样的 Python 库,或者也许有人已经拥有了。

回答by Z. Qui

asyncio does not have support for this. However, aiofilessupports just this. Please have a look.

asyncio 不支持这个。但是, aiofiles仅支持这一点。请看一看。