什么是阻塞功能?

发布于 2024-12-04 10:01:13 字数 83 浏览 1 评论 0原文

什么是阻塞函数阻塞调用

当我提到 Node.js 或实时处理语言时,我一次又一次地看到这个术语。

What is a blocking function or a blocking call?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖个坑埋了你 2024-12-11 10:01:13

停止脚本执行直至结束的函数。

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

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

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

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

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

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

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

Do other stuff
Wrote to file!

这使得应用程序非常快速,因为您无需等待客户端消息、文件写入或其他消息。您可以继续以并行方式处理数据。这被许多人认为是 Node.js 的优势之一。

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

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

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

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!

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.

泅人 2024-12-11 10:01:13
var block = function _block() {
  while(true) {
    readInputs();
    compute();
    drawToScreen();
  }
}

阻塞函数基本上会永远计算。这就是阻塞的意思。

其他阻塞函数将等待 IO 发生,

非阻塞 IO 系统意味着函数启动 IO 操作,然后进入空闲状态,然后在发生时处理 IO 操作的结果。

这基本上是线程空闲和睡眠之间的区别。

var block = function _block() {
  while(true) {
    readInputs();
    compute();
    drawToScreen();
  }
}

A blocking function basically computes forever. That's what it means by blocking.

Other blocking functions would wait for IO to occur

a non-blocking IO system means a function starts an IO action, then goes idle then handles the result of the IO action when it happens.

It's basically the difference between a thread idling and sleeping.

西瓜 2024-12-11 10:01:13

阻塞调用是一种在返回到调用线程之前不允许处理继续的调用 - 这也称为同步调用。另一方面,异步意味着线程(和代码)可以同时(并发)执行。

A blocking call is one that doesn't allow processing to continue until it returns to the calling thread - this is also referred to as a synchronous call. Asynchronous on the other hand means that threads (and code) can execute at the same time (concurrently).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文