【发布时间】:2017-03-08 08:44:44
【问题描述】:
最近我想在本地文件 IO 上使用 Python async/await,但是在阅读以下链接后我发现这是不可能的:
Does asyncio supports asynchronous I/O for file operations?
Read file line by line with asyncio
解决方案是基于线程的 aiofiles 模块。但是在 Nodejs 中,仅使用基于标准 POSIX 函数的 fs 模块来使文件 IO 异步变得如此完美和容易。为什么nodejs可以做I/O异步,python却做不到?
【问题讨论】:
-
Nodes 并不一定比 Python 做更多的“asyncio”。 Nodejs 所做的是捆绑了一个 FS/IO API,默认情况下,它鼓励在 IO 调用上使用异步模式。
-
asyncio并非设计用于执行异步文件 I/O。在 python 中还有其他库可以做到这一点。例如aiofiles,它使用 fs API 扩展了 asyncio。
标签: python node.js asynchronous