2

I know fs.writeFile() is asynchronous and will be delayed to be executed.

For example:

fs.writeFile()
console.log("non-blocking") // last statement of an event handler function

will print non-blocking immediately and then that thread will spend time to execute the operations to write files.

However, if the NodeJS App is single-threaded. When the thread starts to execute the operations to write files after executing console.log("non-blocking"). The thread seems still being blocked, though the blocking status is delayed. The blocking should happen before console.log("non-blocking") if fs.writeFile is replaced by fs.writeFileSync. When I use fs.writeFile here, the blocking still seems to exist when the last statement of that event handler is finished and the file writing operation is started.

Is it true that the thread is blocked when the file-writing operations are started? Or the blocking will happen in the main thread anyway. If so, is there a way to avoid it?


I don't think the question is the same as How the single threaded non blocking IO model works in Node.js, because this question is mainly about how NodeJS interacts with Operation System to do file operations.

Community
  • 1
  • 1
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
  • What you really ask is unclear, especially what you mean with "delayed" "blocking status". There's only one user thread handling events one at a time, but the nodejs app contains other threads. – Denys Séguret Apr 29 '15 at 09:39
  • @dystroy Do you mean `fs.writeFile()` is handled by other threads in the thread pool instead of the main event-loop thread? If it is handled by the main thread, the main thread seems to be blocked anyway.. – Hanfei Sun Apr 29 '15 at 09:42
  • Of course, it's handled natively in another thread of the node app and events wake up the JS user thread, one at a time. – Denys Séguret Apr 29 '15 at 09:43
  • "if `fs.writeFIle` is synchronous" `fs.writeFile` is **never** synchronous, there's a reason `fs.writeFileSync` exists. – Ben Fortune Apr 29 '15 at 09:44
  • @dystroy Thanks! When you say the file operations is handled by other threads and handled natively, is there any document to explain how NodeJS send requests to the "file-writing thread"? – Hanfei Sun Apr 29 '15 at 09:46
  • @hanfeisun ultimately, `writeFile` is handled by the OS which itself is multi-threaded. Node just listens for a notification once the operation is finished so it can trigger it's callback (if provided). So to answer your question, in the context of your Node app, no, `writeFile` will *never* block. However, when `writeFile` is handled by the OS, yes, on the thread where it runs it will block but that is outwith the scope of your app. – James Apr 29 '15 at 09:49
  • possible duplicate of [How the single threaded non blocking IO model works in Node.js](http://stackoverflow.com/questions/14795145/how-the-single-threaded-non-blocking-io-model-works-in-node-js) – Denys Séguret Apr 29 '15 at 09:54
  • @James Thanks! If there is a "2 Gigabytes string" need to be written to a file, is it necessary for NodeJS to send this String to that "OS file-writing thread"? If so, will this String-sending process be time-consuming and blocking? – Hanfei Sun Apr 29 '15 at 10:00
  • You don't usually use "2 Gigabytes string" but streams or chunks. – Denys Séguret Apr 29 '15 at 10:03
  • @hanfeisun you wouldn't write data that big in one operation, as explained by dystroy you would stream/pipe the data in chunks to the file (this would usually be done by handling events raised by the API). – James Apr 29 '15 at 10:10

1 Answers1

3

There's only one thread... for your code. The nodejs application contains/calls other threads doing the operations (mainly IO) for you and calling your user thread on events.

When you do fs.writeFile, the action is given to another native thread which handles the operation. If you gave a callback to fs.writeFile, your user thread will be awoken to execute it. The core of this asynchronous IO operations management is libuv.

Here's a document explaining that even model.

EDIT: I voted to close, as there's another more complete QA on this topic.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Thanks! One more question :-) if my NodeJS app is single-threaded, who (or which thread) is responsible to awake the main thread to execute the callback to `fs.writeFile` – Hanfei Sun Apr 29 '15 at 10:21
  • 1
    Your nodejs application **isn't** single-threaded. There's one JS thread but many other threads in the application and the OS handling your IO tasks. But you really should consider this as an implementation detail, because it is. – Denys Séguret Apr 29 '15 at 10:27