Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

how do i create a non-blocking asynchronous function in node.js?

How do I create a non-blocking asynchronous function? Below is what I'm trying to achieve but my program is still blocking...

var sys = require("sys");

function doSomething() {
  sys.puts("why does this block?");
  while(true);
}

setTimeout(doSomething,0);
setTimeout(doSomething,0);
setTimeout(doSomething,0);

sys.puts("main");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Cross-posted from Reddit.


The purpose of asynchronous functions in JavaScript is a little bit different from what you seek.

Remember that JavaScript is single-threaded — it can only do one thing at a time. Here is some traditional, blocking code:

sys.puts("Before");
sleep(10);
sys.puts("After");

In a real-world web application, the sleep() might instead be a time-consuming database call, network request (like waiting for data from the user’s web browser), helper tool, or file access.

If you used blocking calls like the above, the Node.js server would’t be able to do anything else (like starting to handle other web requests) while waiting.

PHP and many other web programming environments handle this by creating totally separate threads for each request. Node.js uses callback functions. You could write the same code like this, instead:

sys.puts("Before");
setTimeout(function(){
    sys.puts("After");
}, 10000);

Here, you create a function and pass it to setTimeout(). Its code hasn’t run yet, but when it does, it will have access to all the scope (all the variables) where it was created. setTimeout() gets a reference to the function and schedules an event to fire on the event loop after the timeout expires.

The event loop is, essentially, a Node.js program’s to-do list (they’re common — all the GUI applications running on your computer probably use event loops!).

After the call to setTimeout(), the current function keeps executing. It eventually returns, and the function which called it returns, and so on, until the program ends up back in the event loop. The event loop looks to see if anything has happened (e.g. an incoming request) while your code was executing, and calls the appropriate function in your code. If not, it waits until something does happen (like the timeout expiring).

Asynchronous code doesn’t let your code do many things at the same time, it does eliminate blocking when some code depends on something external to continue.

It’s rare that you need to do blocking work inside your Node.js program. If you do, you should separate that work out into a separate process (which can even be another Node.js program), or write a C/C++ addon which is free to use threads.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...