stdio 的同步阻塞


🌐 Synchronous blocking of stdio

Worker 通过 <MessagePort> 使用消息传递来实现与 stdio 的交互。这意味着,来自 Workerstdio 输出可能会被接收端的同步代码阻塞,从而阻塞 Node.js 事件循环。

import {
 Worker,
 isMainThread,
} from 'node:worker_threads';
if (isMainThread) {
 new Worker(new URL(import.meta.url));
 for (let n = 0; n < 1e10; n++) {
 // Looping to simulate work.
 }
} else {
 // This output will be blocked by the for loop in the main thread.
 console.log('foo');
}const {
 Worker,
 isMainThread,
} = require('node:worker_threads');
if (isMainThread) {
 new Worker(__filename);
 for (let n = 0; n < 1e10; n++) {
 // Looping to simulate work.
 }
} else {
 // This output will be blocked by the for loop in the main thread.
 console.log('foo');
}

AltStyle によって変換されたページ (->オリジナル) /