我如何在node.js中创建一个非阻塞异步函数? [英] how do i create a non-blocking asynchronous function in node.js?

查看:84
本文介绍了我如何在node.js中创建一个非阻塞异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建非阻塞异步函数?以下是我要实现的目标,但是我的程序仍然受阻...

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");

解决方案

交叉发布于 Reddit .


JavaScript中异步函数的用途与您所追求的略有不同.

请记住,JavaScript是单线程的-一次只能做一件事.这是一些传统的阻止代码:

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

在实际的Web应用程序中,sleep()可能是费时的数据库调用,网络请求(例如,等待用户Web浏览器中的数据),帮助工具或文件访问.

如果您使用了如上所述的阻止调用,则在等待时Node.js服务器将无法执行其他任何操作(例如开始处理其他Web请求).

PHP和许多其他Web编程环境通过为每个请求创建完全独立的线程来处理此问题. Node.js使用回调函数.您可以这样编写相同的代码,而不是:

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

在这里,您创建一个函数并将其传递给setTimeout().它的代码尚未运行,但是当它运行时,它将可以访问创建它的所有作用域(所有变量). setTimeout()获取对该函数的引用,并安排在超时到期后在事件循环上触发事件. >

从本质上讲,事件循环是Node.js程序的待办事项列表(它们很常见-计算机上运行的所有GUI应用程序都可能使用事件循环!).

在调用setTimeout()之后,当前函数继续执行.最终,它返回,并且调用它的函数返回,依此类推,直到程序在事件循环中返回.事件循环可查看代码执行过程中是否发生了任何事情(例如传入请求),并在代码中调用适当的函数.如果没有,它会等到发生发生(例如超时到期).

异步代码允许您的代码同时执行许多操作, 可以消除某些代码依赖于外部代码继续执行时的阻塞.

很少需要在Node.js程序中进行阻止工作.如果这样做,则应将其分解为一个单独的过程(甚至可以是另一个Node.js程序),或编写

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.

这篇关于我如何在node.js中创建一个非阻塞异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆