Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sleep the thread in node.js without affecting other threads?

As per Understanding the node.js event loop, node.js supports a single thread model. That means if I make multiple requests to a node.js server, it won't spawn a new thread for each request but will execute each request one by one. It means if I do the following for the first request in my node.js code, and meanwhile a new request comes in on node, the second request has to wait until the first request completes, including 5 second sleep time. Right?

var sleep = require('sleep');     sleep.sleep(5)//sleep for 5 seconds 

Is there a way that node.js can spawn a new thread for each request so that the second request does not have to wait for the first request to complete, or can I call sleep on specific thread only?

like image 659
emilly Avatar asked Nov 19 '12 05:11

emilly


People also ask

Does thread sleep block other threads?

Sleep method. Calling the Thread. Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread. Once that interval elapses, the sleeping thread resumes execution.

Does thread sleep create a new thread?

Java Thread Sleep important points For a quiet system, the actual time for sleep is near to the specified sleep time but for a busy system it will be little bit more. Thread sleep doesn't lose any monitors or locks current thread has acquired.

What happens when thread sleeps?

Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.


2 Answers

If you are referring to the npm module sleep, it notes in the readme that sleep will block execution. So you are right - it isn't what you want. Instead you want to use setTimeout which is non-blocking. Here is an example:

setTimeout(function() {   console.log('hello world!'); }, 5000); 

For anyone looking to do this using es7 async/await, this example should help:

const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));  const example = async () => {   console.log('About to snooze without halting the event loop...');   await snooze(1000);   console.log('done!'); };  example(); 
like image 55
David Weldon Avatar answered Nov 10 '22 17:11

David Weldon


In case you have a loop with an async request in each one and you want a certain time between each request you can use this code:

   var startTimeout = function(timeout, i){         setTimeout(function() {             myAsyncFunc(i).then(function(data){                 console.log(data);             })         }, timeout);    }     var myFunc = function(){         timeout = 0;         i = 0;         while(i < 10){             // By calling a function, the i-value is going to be 1.. 10 and not always 10             startTimeout(timeout, i);             // Increase timeout by 1 sec after each call             timeout += 1000;             i++;         }     } 

This examples waits 1 second after each request before sending the next one.

like image 35
Jodo Avatar answered Nov 10 '22 17:11

Jodo