5

Recently I have wanted to use Python async/await on local file IO, however I find it's impossible after reading following links:

Does asyncio supports asynchronous I/O for file operations?

Read file line by line with asyncio

The solution is the aiofiles modules, which is based on threads. But in Nodejs it's so perfect and easy to make file IO async just using fs modules which are based on standard POSIX functions. Why can't python do I/O async when nodejs can?

Community
  • 1
  • 1
kongkongyzt
  • 190
  • 1
  • 10
  • 2
    Nodes doesn't necessarily do "asyncio" any more than Python does. What Nodejs does is it bundles a FS/IO API that, by default, encourages the use of *an asynchronous pattern* over IO calls. – user2864740 Oct 26 '16 at 05:57
  • 1
    `asyncio` isn't designed to do async file I/O. There are other libraries that do it in python. [aiofiles](https://github.com/Tinche/aiofiles) for example, which extends asyncio with fs APIs. – tcooc Oct 27 '16 at 20:25

1 Answers1

8

But Node.js async file I/O is also based on threads:

Note that all file system APIs except fs.FSWatcher() and those that are explicitly synchronous use libuv's threadpool, which can have surprising and negative performance implications for some applications, see the UV_THREADPOOL_SIZE documentation for more information.

– from https://nodejs.org/api/fs.html#fs_threadpool_usage

So Node.js fs API is doing the same thing as Python asyncio + aiofiles module.

Messa
  • 24,321
  • 6
  • 68
  • 92