Skip to content

Commit 169b652

Browse files
authored
feat: add main process naming
2 parents d57a4dc + dde6589 commit 169b652

34 files changed

+2135
-684
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { spawn as _spawn } from 'node:child_process';
2+
import { Worker, threadId } from 'node:worker_threads';
3+
4+
const spawn = (...args) => _spawn(...args);
5+
6+
console.log('Main PID:', process.pid, 'TID:', threadId);
7+
8+
const childScript =
9+
"const { threadId: t } = require('worker_threads'); console.log('spawn PID:', process.pid, 'TID:', t);";
10+
const children = [
11+
spawn(process.execPath, ['-e', childScript], { stdio: 'inherit' }),
12+
spawn(process.execPath, ['-e', childScript], { stdio: 'inherit' }),
13+
];
14+
15+
const workerScript =
16+
"const { threadId: t } = require('worker_threads'); console.log('Worker PID:', process.pid, 'TID:', t);";
17+
const workers = [
18+
new Worker(workerScript, { eval: true }),
19+
new Worker(workerScript, { eval: true }),
20+
];
21+
22+
let exited = 0;
23+
function checkDone() {
24+
exited++;
25+
if (exited === children.length + workers.length) {
26+
process.exit(0);
27+
}
28+
}
29+
30+
[...children, ...workers].forEach((child) => {
31+
child.on('exit', checkDone);
32+
});

0 commit comments

Comments
 (0)