3

For the below code,

var fs = require('fs');

fs.watch('target.txt', function(event, fileName){
    console.log('Event: ' + event + ', for file: ' + fileName);
    });

Console.log('Now watching target.txt');

As per the below architecture,

1) fs.watch() will invoke libuv. libuv will launch a thread to track change event on target.txt. The result from libuv will go to v8 and again through NodeJS Bindings in the form of callback with a buffer having data.

2) libuv adds change event in Event queue. As the event loop picks the change event, corresponding call back is executed in v8 run time.

enter image description here

Is my understanding correct?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • I do not understand down-voters. The op misunderstands Node, but this is a common misunderstanding, so this question will actually prove to be helpful to many people who begin to use NodeJS – Lajos Arpad May 05 '16 at 13:48

1 Answers1

1

No, you misunderstand it. NodeJS does not have threads, it is single-threaded instead, using the Observer Pattern. The event loop waits for events to occur (to observe an event). When an event is occurred, then it calls its handler. The illusion of multi-threaded approach comes from the fact that Node frequently uses async events, defining callback functions to be executed when a given task is finished. Read more here.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • @overexchange, "Node js is a single threaded application but it support concurrency via concept of event and callbacks." http://www.tutorialspoint.com/nodejs/nodejs_event_loop.htm – Lajos Arpad May 05 '16 at 15:27
  • Ok, I wrote python code, similar to [this](https://github.com/shamhub/Design_pattern/tree/master/EventHandling-Observer_pattern). – overexchange Jul 28 '17 at 10:18
  • Did this answer help you in solving your problem? – Lajos Arpad Jul 28 '17 at 12:57
  • 1
    As you said: *it is single-threaded instead, using the Observer Pattern.*. Can you verify [this](https://github.com/shamhub/Design_pattern/tree/master/EventHandling-Observer_pattern) code that implements it? – overexchange Jul 28 '17 at 20:02
  • I am not a Python expert, but the code seems to be a nice implementation at first sight. – Lajos Arpad Jul 29 '17 at 08:16
  • How does the task scheduler know that task is finished? assuming task can be any call on wire – overexchange Jul 29 '17 at 10:17
  • Supposedly there is a task Queue and whenever a new task is scheduled, it is pushed into the queue, while when it is executed, it is popped from the Queue. So the part which executes the tasks needs to remove the task being executed from the Queue. – Lajos Arpad Jul 29 '17 at 11:20
  • No, this model is not multithreaded. It periodically checks whether there is a task to be executed and if so, it executes. No threads, nothing like that, just an infinite cycle of a task manager. – Lajos Arpad Jul 31 '17 at 08:39
  • When it executes the task, what happens when task goes on IO wait? How task scheduler gets its control back? Because this is single threaded – overexchange Jul 31 '17 at 10:06
  • NodeJS has an event loop for this purpose, kindly read this article for more information: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ – Lajos Arpad Aug 01 '17 at 10:08