什么是阻止功能? [英] What is a blocking function?

查看:105
本文介绍了什么是阻止功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是阻止功能阻止调用?

这是我在提及Node.js或实时处理语言时一再看到的术语.

This is a term I see again and again when referring to Node.js or realtime processing languages.

推荐答案

一个函数,它将停止脚本执行直到结束.

A function that stops script execution until it ends.

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

For example, if I had a function in my language that was used to write to a file, like so:

fwrite(file, "Contents");
print("Wrote to file!");

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

The print statement would only be executed once the file has been written to the disk. The whole program is halted on this instruction. This isn't noticeable for small enough writes, but imagine I had a huge blob to write to the file, one that took many seconds:

fwrite(file, blob);
print("Wrote to file!");

print语句仅在写几秒钟后才执行,并且整个程序将在该时间停止.在Node.js中,这些操作是通过事件回调 异步完成的.我们的示例将变为:

The print statement would only be executed after a few seconds of writting, and the whole program would be stopped for that time. In Node.js, this stuff is done asynchronously, using events and callbacks. Our example would become:

fwrite(file, blob, function() {
    print("Wrote to file!");
});
print("Do other stuff");

第三个参数是写入文件后要调用的函数.无论是否已写入文件,位于write函数之后的print语句都会立即被调用.因此,如果我们要编写一个足够大的Blob,则输出可能如下所示:

Where the third parameter is a function to be called once the file has been written. The print statement located after the write function would be called immediately after, whether or not the file has been written yet. So if we were to write a huge enough blob, the output might look like this:

Do other stuff
Wrote to file!

这使申请变得非常快速,因为您不必等待客户端消息,文件写入或其他操作.您可以继续并行处理数据. Node.js的众多优势之一都考虑到了这一点.

This makes applictions very fast because you're not waiting on a client message, a file write or other. You can keep on processing the data in a parallel manner. This is considered by many one of the strengths of Node.js.

这篇关于什么是阻止功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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