同步和异步编程有什么区别(在node.js中)

我一直在阅读 nodebeginner我遇到了以下两段代码.

第一个:

 var result = database.query("SELECT * FROM hugetable");console.log("Hello World");

第二个:

 database.query("SELECT * FROM hugetable", function(rows) {变量结果 = 行;});console.log("Hello World");

我明白他们应该做什么,他们查询数据库以检索查询的答案.然后是 console.log('Hello world').

第一个应该是同步代码.第二个是异步代码.

这两部分的区别对我来说很模糊.输出会是什么?

谷歌搜索异步编程对我也没有帮助.

解决方案

不同的是,在第一个例子中,程序会在第一行阻塞.下一行 (console.log) 将不得不等待.

在第二个示例中,console.log 将在处理查询时执行.也就是说,查询将在后台处理,而您的程序正在做其他事情,一旦查询数据准备好,您将随心所欲.

所以,简而言之:第一个示例会阻塞,而第二个不会.

以下两个例子的输出:

//示例 1 - 同步(块)var result = database.query("SELECT * FROM hugetable");console.log("查询完成");console.log("下一行");//示例 2 - 异步(不阻塞)database.query("SELECT * FROM hugetable", function(result) {console.log("查询完成");});console.log("下一行");

应该是:

  1. 查询完成
    下一行
  2. 下一行
    查询完成

注意
虽然 Node 本身是单线程,但也有一些任务可以并行运行.例如,文件系统操作发生在不同的进程中.

这就是 Node 可以执行异步操作的原因:一个线程执行文件系统操作,而 Node 主线程继续执行您的 javascript 代码.在像 Node 这样的事件驱动服务器中,文件系统线程通知主 Node 线程某些事件,例如完成、失败或进度,以及与该事件相关的任何数据(例如数据库查询的结果或错误消息)并且主节点线程决定如何处理该数据.

您可以在此处阅读更多相关信息:单线程非阻塞 IO 模型在 Node.js 中的工作原理p>

I've been reading nodebeginner And I came across the following two pieces of code.

The first one:

    var result = database.query("SELECT * FROM hugetable");
    console.log("Hello World");

The second one:

    database.query("SELECT * FROM hugetable", function(rows) {
       var result = rows;
    });
    console.log("Hello World");

I get what they're supposed to do, they query the database to retrieve the answer to the query. And then console.log('Hello world').

The first one is supposedly synchronous code. And the second one is asynchronous code.

The difference between the two pieces is very vague to me. What would the output be?

Googling on asynchronous programming didn't help me either.

解决方案

The difference is that in the first example, the program will block in the first line. The next line (console.log) will have to wait.

In the second example, the console.log will be executed WHILE the query is being processed. That is, the query will be processed in the background, while your program is doing other things, and once the query data is ready, you will do whatever you want with it.

So, in a nutshell: The first example will block, while the second won't.

The output of the following two examples:

// Example 1 - Synchronous (blocks)
var result = database.query("SELECT * FROM hugetable");
console.log("Query finished");
console.log("Next line");


// Example 2 - Asynchronous (doesn't block) 
database.query("SELECT * FROM hugetable", function(result) {
    console.log("Query finished");
});
console.log("Next line");

Would be:

  1. Query finished
    Next line
  2. Next line
    Query finished

Note
While Node itself is single threaded, there are some task that can run in parallel. For example, File System operations occur in a different process.

That's why Node can do async operations: one thread is doing file system operations, while the main Node thread keeps executing your javascript code. In an event-driven server like Node, the file system thread notifies the main Node thread of certain events such as completion, failure, or progress, along with any data associated with that event (such as the result of a database query or an error message) and the main Node thread decides what to do with that data.

You can read more about this here: How the single threaded non blocking IO model works in Node.js

相关文章