File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -1191,6 +1191,45 @@ active handle in the event system. If the worker is already `unref()`ed calling
11911191
11921192## Notes
11931193
1194+ ### Synchronous blocking of stdio
1195+
1196+ ` Worker ` s utilize message passing via {MessagePort} to implement interactions
1197+ with ` stdio ` . This means that ` stdio ` output originating from a ` Worker ` can
1198+ get blocked by synchronous code on the receiving end that is blocking the
1199+ Node.js event loop.
1200+
1201+ ``` mjs
1202+ import {
1203+ Worker ,
1204+ isMainThread ,
1205+ } from ' worker_threads' ;
1206+
1207+ if (isMainThread) {
1208+ new Worker (new URL (import .meta.url));
1209+ for (let n = 0 ; n < 1e10 ; n++ ) {}
1210+ } else {
1211+ // This output will be blocked by the for loop in the main thread.
1212+ console .log (' foo' );
1213+ }
1214+ ` ` `
1215+
1216+ ` ` ` cjs
1217+ ' use strict' ;
1218+
1219+ const {
1220+ Worker ,
1221+ isMainThread ,
1222+ } = require (' worker_threads' );
1223+
1224+ if (isMainThread) {
1225+ new Worker (__filename );
1226+ for (let n = 0 ; n < 1e10 ; n++ ) {}
1227+ } else {
1228+ // This output will be blocked by the for loop in the main thread.
1229+ console .log (' foo' );
1230+ }
1231+ ` ` `
1232+
11941233### Launching worker threads from preload scripts
11951234
11961235Take care when launching worker threads from preload scripts (scripts loaded
You can’t perform that action at this time.
0 commit comments