什么是阻塞功能?
什么是阻塞函数或阻塞调用?
当我提到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
停止脚本执行直至结束的函数。
例如,如果我的语言有一个用于写入文件的函数,如下所示:
print
语句仅在文件写入磁盘后才会执行。整个程序根据此指令停止。对于足够小的写入来说,这并不明显,但想象一下我有一个巨大的 blob 需要写入文件,这需要花费很多秒:print
语句只会在写入几秒钟后执行,到那时整个程序就会停止。在 Node.js 中,这些事情是使用事件和回调异步完成的。我们的示例将变为:其中第三个参数是写入文件后要调用的函数。无论文件是否已写入,位于写入函数之后的
print
语句都会立即被调用。因此,如果我们要编写足够大的 blob,输出可能如下所示:这使得应用程序非常快速,因为您无需等待客户端消息、文件写入或其他消息。您可以继续以并行方式处理数据。这被许多人认为是 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:
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: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: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: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.
阻塞函数基本上会永远计算。这就是阻塞的意思。
其他阻塞函数将等待 IO 发生,
非阻塞 IO 系统意味着函数启动 IO 操作,然后进入空闲状态,然后在发生时处理 IO 操作的结果。
这基本上是线程空闲和睡眠之间的区别。
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.
阻塞调用是一种在返回到调用线程之前不允许处理继续的调用 - 这也称为同步调用。另一方面,异步意味着线程(和代码)可以同时(并发)执行。
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).