-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node Js cluster: process fork is not working #5323
Comments
Interesting. I'm not sure what's happening yet, but at least ts-node supports this somehow, so maybe it's possible for vite-node too. I made a reproduction on stackblitz: $ npx ts-node index.ts
[isPrimary] {
args: [],
exec: '/home/projects/rlmixvjzk.github/index.ts',
execArgv: [
'/home/projects/rlmixvjzk.github/node_modules/ts-node/dist/bin.js'
],
silent: false
}
[isWorker]
[isWorker.msg] 0
$ npx vite-node index.ts
[isPrimary] {
args: [],
exec: '/home/projects/rlmixvjzk.github/node_modules/.bin/vite-node',
execArgv: [],
silent: false
}
No files specified.
vite-node/1.3.1
Usage:
$ vite-node [...files]
... |
It looks like Currently For the time being, I think you can use https://stackblitz.com/edit/github-kfifdx?file=index-fix.ts cluster.setupPrimary({
args: [import.meta.filename],
}) |
it started working but I'm not able to send messages from parent to child import cluster from 'cluster';
if (cluster.isPrimary) {
cluster.setupPrimary({
args: [import.meta.filename],
});
console.log('[isPrimary] settings', cluster.settings);
const worker = cluster.fork();
worker.send('hello from primary')
worker.on('message', (message) => {
console.log('[isPrimary] received :', { message });
});
} else if (cluster.isWorker) {
const message = 'hello from worker!';
console.log('[isWorker] sending :', { message });
process.on('message', (msg)=> {
console.log('child ', msg)
})
process.send(message);
} |
Yeah, I noticed that in your example, but I assumed that's simply because of a race condition and you probably need to wait until worker side https://stackblitz.com/edit/github-kfifdx-3xmjq2?file=index-fix.ts |
@hi-ogawa Thanks! I tested the same code on |
Yeah, I understand and also plain Node works with this. But, the reason why they work might be just a lucky since there's no guarantee how long it takes from parent's I think usual practice is to put some retry mechanism to ensure initial hand-shake to counter against such race condition. Or actually waiting for a message from a child to a parent seems better since this direction is probably guaranteed to be race condition free (cf. https://stackoverflow.com/a/66190604) Btw, keeping this issue open is fine since the workaround |
Describe the bug
I'm trying to use the
cluster
module of node js. I tried running my code using node it's working perfectly but when I'm trying to run the same code usingnpx vite-node ./file-path
it works forprimary
cluster but when it comes to worker threads it saysNo files specified.
Output:
invoked primary thread - No files specified. vite-node/1.3.1 Usage: $ vite-node [...files] Commands: [...files] For more info, run any command with the `--help` flag: $ vite-node --help Options: -r, --root <path> Use specified root directory -c, --config <path> Use specified config file -m, --mode <mode> Set env mode -w, --watch Restart on file changes, similar to "nodemon" --script Use vite-node as a script runner --options <options> Use specified Vite server options -v, --version Output the version number -h, --help Display help for command -v, --version Display version number ... so on
Expected Output:
Reproduction
To reproduce bug,
yarn init -y
index.ts
index.ts
npx vite-node ./index.ts
System Info
The text was updated successfully, but these errors were encountered: