Advertisement
Guest User

Main

a guest
Jan 25th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { StaticPool } = require("node-worker-threads-pool");
  2.  
  3. const filePath = "./w.js";
  4.  
  5. async function solve(x){
  6.  
  7.   let cnt = 0;
  8.   let iterNum = 7;
  9.  
  10.   const pool = new StaticPool({
  11.     size: 2,
  12.     task: filePath,
  13.     workerData: "workerData!",
  14.   });
  15.  
  16.   for (let i = 0; i < iterNum; i++) {
  17.     (async () => {
  18.       const num = x   + Math.trunc(10 * Math.random());
  19.  
  20.       // This will choose one idle worker in the pool
  21.       // to execute your heavy task without blocking
  22.       // the main thread!
  23.       const res = await pool.exec(num);
  24.  
  25.       console.log(`Fibonacci(${num}) result:`, res);
  26.      
  27.       cnt += 1;
  28.       if(cnt == iterNum){
  29.         pool.destroy()
  30.       }
  31.  
  32.     })();
  33.   }
  34.   return 0;
  35. }
  36.  
  37. xs = [25,30,35];
  38. js = [0,1,2];
  39.  
  40. async function run (js){
  41.   for (const j of js) {
  42.     console.log(`${j} iteration.`);
  43.     const r = await solve(xs[j]);
  44.   }
  45. }
  46.  
  47. run(js);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement