11

I have studied about the event loop in Node.Js, it work in asynchronous and non-blocking manner to process the request. Is there any way so that we can block the execution of event loop?

3
  • 1
    Yes - have a synchronous code that doesn't exit. That will block it. Commented Apr 22, 2020 at 6:05
  • 1
    Why would you ever want to? Commented Apr 22, 2020 at 6:33
  • 5
    @LasseV.Karlsen - It's more likely one wants to know what might block the event loop so you can avoid doing so. Commented Apr 22, 2020 at 6:37

2 Answers 2

21

There are lots of ways to block the event loop. Some ways block it just for awhile (like using synchronous file I/O) and some ways block it forever.

For example, this blocks it forever:

let flag = false;
setTimeout(() => {
    // this callback never gets called
    // because event loop is blocked
    flag = true;
}, 1000);

while (!flag) {
    console.log("still waiting")
}
// never get here

The issue is that the while() loop runs until the flag changes value. As long as that while loop is running, the event loop is blocked. There's a setTimeout() that wants to fire in 1 second, but it can't actually call its callback until the interpreter gets back to the event loop. But, it won't get back to the event loop until the while() loop is done. It's a deadlock which results in an infinite loop and the event loop is permanently blocked.

The setTimeout() can't call its callback until the while loop is done and the while loop won't finish until the setTimeout() runs its callback. Deadlock, infinite loop.


This blocks it for awhile while all the file operations are going on and all the processing of the files:

setTimeout(() => {
    // this doesn't get to run until all the synchronous file I/O 
    // finishes in the code below, even though the timer is set
    // for only 10ms
    console.log("finally got to run the timer callback");
}, 10);

let files = some array of files;
for (let file of files) {
    let data = fs.readFileSync(file);
    let lines = data.split("\n");
    for (let line of lines) {
        // do something
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes got it with these examples.
-1

Event loop runs on a thread so any call that blocks the thread, like an infinite loop, or a sync file operation, or a blocking network call, will block the event loop.

You can imagine the event loop as first in first out callback queue, if one function call is stuck, subsequent calls will wait for it to finish.

You can also block the event loop by infinitely queuing a new task to the end using setTimeout or setInterval functions.

5 Comments

http call doesn't block the thread in node js, at least "from the box"
@AlexanderDanilov I wasn't talking about node's http module but a network call.
http network call doesn't block any thread
@AlexanderDanilov What do you mean by crappy? The warning is for not because the code is crappy, it is because blocking a server is a serious issue with certain ramifications. But, that is not the point, the point is you can block on a network call. Again, I am not talking about Node's net module but in general. Just for assurance please look for the statement "then read calls will block indefinitely" on this page doc.rust-lang.org/stable/std/net/struct.TcpStream.html. Sorry I did not get the part 'I wrote "from the box"'.
So now after your edit it seems to be a duplicate of @jfriend00 answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.