简体   繁体   English

NodeJS 中的异步与同步

[英]Asynchronous Vs synchronous in NodeJS

I'm new to NodeJS.我是 NodeJS 的新手。 I have seen there are separate asynchronous and synchronous functions for the same task (ex: { fs.writeFile,fs.writeFileSync } , { fs.read, fs.readSync }).我已经看到同一任务有单独的异步和同步函数(例如:{ fs.writeFile,fs.writeFileSync } , { fs.read, fs.readSync })。

Can anyone explain why is that?谁能解释一下这是为什么? and what is the difference?有什么区别?

Async:异步:

  1. Send request发送请求
  2. go on with other code继续其他代码
  3. response come in any time on a callback响应在回调中随时出现

Sync:同步:

  1. Send request发送请求
  2. Wait for response等待回应
  3. go on with other code after response响应后继续使用其他代码

The reason for having both sync and async verisons of those operations is that they can be time-consuming.使用这些操作的同步和异步版本的原因是它们可能很耗时。 Since node.js has a single-threaded main event loop, you do not under any circumstances want to block the event loop with slow synchronous function calls.由于 node.js 有一个单线程主事件循环,你在任何情况下都不希望用慢速同步函数调用阻塞事件循环。

That is why everything is done using callbacks/promises/futures instead.这就是为什么一切都是使用回调/承诺/期货来完成的。 This way, you can have an event loop that just calls an async function and handle the result of the async function in a callback, when it happens to be done.这样,您可以有一个事件循环,它只调用异步函数并在回调中处理异步函数的结果,当它恰好完成时。

This is one of the major strengths of node.js, and one of the basic rules: "do not block the main event loop".这是 node.js 的主要优势之一,也是基本规则之一:“不要阻塞主事件循环”。

In reference to node.js fs.writeFile doc , it asynchronously writes data to a file.参考node.js fs.writeFile doc ,它将数据异步写入文件。 This means if you have following code:这意味着如果您有以下代码:

1. some db operation
2. some non-related db operation

In such case, the 1st operation will not block 2nd operation.在这种情况下,第一个操作不会阻止第二个操作。 2nd operation execute immediately after 1st(without waiting to finish)第二个操作在第一个之后立即执行(无需等待完成)

However, there are some scenarios like:但是,有一些场景,例如:

1. some db operation 
2. some related db operations(which you can't put in callbacks) and forcefully want to be after 1st operation.

Then use fs.writeFileSync .然后使用fs.writeFileSync

Synchronous is a blocking call, where the thread is blocked until that call is over.同步是阻塞调用,线程被阻塞直到调用结束。 Asynchronous is a non-blocking call, where the thread continues to execute the rest, why the call is executed separately.异步是非阻塞调用,线程继续执行剩下的,为什么要单独执行调用。

Quoting from NodeJS Documentations :引用NodeJS 文档

Blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes.阻塞是指 Node.js 进程中额外 JavaScript 的执行必须等到非 JavaScript 操作完成。 This happens because the event loop is unable to continue running JavaScript while a blocking operation is occurring.发生这种情况是因为在发生阻塞操作时,事件循环无法继续运行 JavaScript。

Blocking methods execute synchronously and non-blocking methods execute asynchronously .阻塞方法同步执行,非阻塞方法异步执行。

The use of Async methods Vs Sync methods:异步方法与同步方法的使用:

If you call is an extensive operation like an I/O operation (Files, DB accesses etc), do use Async methods, because you don't want to block the entire process while and extensive operation is being carried out.如果您调用的是像 I/O 操作(文件、数据库访问等)这样的扩展操作,请使用异步方法,因为您不想在执行扩展操作时阻塞整个过程。

But if it is a regular call, where the result of it, is important for the rest of the process to function, do use Sync methods, where the process will halt until the call is completed.但是如果它是一个常规调用,它的结果对于进程的其余部分的功能很重要,请使用 Sync 方法,在该方法中进程将暂停,直到调用完成。

If you are using AWS lambda for example, making an async call (will do the I/O operation asynchronously), might terminate the Lambda function as soon as the rest of the process is finished.例如,如果您使用 AWS lambda,进行异步调用(将异步执行 I/O 操作)可能会在其余过程完成后立即终止 Lambda 函数。 So it is important identify when to use sync calls and async calls.因此,确定何时使用同步调用和异步调用非常重要。

For more information, read the following doc .有关更多信息,请阅读以下文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM