How to use all my CPU cores with Elysia? #416
-
I have tried to find an example how to use all my CPU cores and couldn't find anything. Can someone share their snippet if possible? I'm interested in Elysia and would like to learn how to push its limits. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I think this is common question for running Bun/Node.js on across all CPUs available, without any code modifications. You can read about how to do it with pm2 - Cluster Mode. Besides, you can use Bun - workers |
Beta Was this translation helpful? Give feedback.
-
Thank you @loclv 👍 |
Beta Was this translation helpful? Give feedback.
-
It seems I did not need to use the Node.js cluster after all; Bun supports spawn which I took advantage of and produced the desired results I wanted from the very beginning. Below is the code which you can place in a import os from 'node:os';
// @ts-ignore
const numCPUs = os.availableParallelism();
for (let i = 0; i < numCPUs; i++) {
Bun.spawn(['bun', 'src/index.ts'], {
stdio: ['inherit', 'inherit', 'inherit'],
env: { ...process.env },
});
} With
What's interesting, without
which does not match the node.js cluster behavior, that is single-threaded result times the number of CPU cores available, in my case 4. Is this an expected behavior? |
Beta Was this translation helpful? Give feedback.
@stefanos82
I think this is common question for running Bun/Node.js on across all CPUs available, without any code modifications.
You can read about how to do it with pm2 - Cluster Mode.
And how to run Bun as a daemon with PM2.
Besides, you can use Bun - workers