什么是阻止功能?

cjm*_*671 9 asynchronous terminology real-time blocking node.js

这是我在提到实时处理语言时一次又一次看到的术语.在这种特殊情况下,我正在阅读node.js主页,它有这样的引用:

"在其他系统中,始终存在阻塞调用以启动事件循环."

什么是拦截电话?

Ale*_*pin 16

一种阻止脚本执行直到结束的函数.

例如,如果我的语言中有一个用于写入文件的函数,如下所示:

fwrite(file, "Contents");
print("Wrote to file!");
Run Code Online (Sandbox Code Playgroud)

print只有在将文件写入磁盘后才会执行该语句.整个程序停止在此指令上.这对于足够小的写入来说并不明显,但是想象一下我有一个巨大的blob要写入文件,这需要花费很多秒:

fwrite(file, blob);
print("Wrote to file!");
Run Code Online (Sandbox Code Playgroud)

print语句只会在写入几秒钟后执行,整个程序将在此时停止.在Node.js中,这些东西是使用事件回调异步完成的.我们的例子将成为:

fwrite(file, blob, function() {
    print("Wrote to file!");
});
print("Do other stuff");
Run Code Online (Sandbox Code Playgroud)

其中第三个参数是一旦写入文件就要调用的函数.在print位于写功能后声明会后立即调用,不管是不是文件已经没有写.因此,如果我们要写一个足够大的blob,输出可能如下所示:

Do other stuff
Wrote to file!
Run Code Online (Sandbox Code Playgroud)

这使得应用程序非常快,因为您不是在等待客户端消息,文件写入或其他.您可以继续以并行方式处理数据.这被Node.js的许多优势所考虑.