File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -968,6 +968,45 @@ active handle in the event system. If the worker is already `unref()`ed calling
968
968
969
969
## Notes
970
970
971
+ ### Synchronous blocking of stdio
972
+
973
+ ` Worker ` s utilize message passing via {MessagePort} to implement interactions
974
+ with ` stdio ` . This means that ` stdio ` output originating from a ` Worker ` can
975
+ get blocked by synchronous code on the receiving end that is blocking the
976
+ Node.js event loop.
977
+
978
+ ``` mjs
979
+ import {
980
+ Worker ,
981
+ isMainThread ,
982
+ } from ' worker_threads' ;
983
+
984
+ if (isMainThread) {
985
+ new Worker (new URL (import .meta.url));
986
+ for (let n = 0 ; n < 1e10 ; n++ ) {}
987
+ } else {
988
+ // This output will be blocked by the for loop in the main thread.
989
+ console .log (' foo' );
990
+ }
991
+ ` ` `
992
+
993
+ ` ` ` cjs
994
+ ' use strict' ;
995
+
996
+ const {
997
+ Worker ,
998
+ isMainThread ,
999
+ } = require (' worker_threads' );
1000
+
1001
+ if (isMainThread) {
1002
+ new Worker (__filename );
1003
+ for (let n = 0 ; n < 1e10 ; n++ ) {}
1004
+ } else {
1005
+ // This output will be blocked by the for loop in the main thread.
1006
+ console .log (' foo' );
1007
+ }
1008
+ ` ` `
1009
+
971
1010
### Launching worker threads from preload scripts
972
1011
973
1012
Take care when launching worker threads from preload scripts (scripts loaded
You can’t perform that action at this time.
0 commit comments