Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can Nodejs do file I/O async while Python asyncio can't?

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?

like image 628
kongkongyzt Avatar asked Oct 26 '16 05:10

kongkongyzt


1 Answers

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.

like image 125
Messa Avatar answered Sep 29 '22 15:09

Messa