4
votes

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?

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
asyncio isn't designed to do async file I/O. There are other libraries that do it in python. aiofiles for example, which extends asyncio with fs APIs. - tcooc

2 Answers

7
votes

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.

-2
votes

use multithreading like this:

import threading
t = threading.Thread(target=method, name='LoopThread')
t.start()
t.join()