Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { StaticPool } = require("node-worker-threads-pool");
- const filePath = "./w.js";
- async function solve(x){
- let cnt = 0;
- let iterNum = 7;
- const pool = new StaticPool({
- size: 2,
- task: filePath,
- workerData: "workerData!",
- });
- for (let i = 0; i < iterNum; i++) {
- (async () => {
- const num = x + Math.trunc(10 * Math.random());
- // This will choose one idle worker in the pool
- // to execute your heavy task without blocking
- // the main thread!
- const res = await pool.exec(num);
- console.log(`Fibonacci(${num}) result:`, res);
- cnt += 1;
- if(cnt == iterNum){
- pool.destroy()
- }
- })();
- }
- return 0;
- }
- xs = [25,30,35];
- js = [0,1,2];
- async function run (js){
- for (const j of js) {
- console.log(`${j} iteration.`);
- const r = await solve(xs[j]);
- }
- }
- run(js);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement