| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c0deeea commit 5c38a55
1 file 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 | |||
| 1191 | 1191 | ||
| 1192 | 1192 | ## Notes | |
| 1193 | 1193 | ||
| 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 | + | ||
| 1194 | 1233 | ### Launching worker threads from preload scripts | |
| 1195 | 1234 | ||
| 1196 | 1235 | Take care when launching worker threads from preload scripts (scripts loaded | |
| Back | FazBrowse Home | New Git URL |
0 commit comments