summaryrefslogtreecommitdiff
path: root/node_modules/webworker-threads/examples/demo.js
blob: 63316aed3cc722f50d9e70887df8db759a5e2052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var http= require('http');

function fibo (n) {
  return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
}

function fast (req,res) {
  process.stdout.write('·');
  res.end("FAST");
}

function slow (req,res) {
  process.stdout.write('•');
  res.end("SLOW -> "+ fibo(40));
}

var numThreads= parseInt(process.argv[2], 10) || 5;
console.log("Using "+ numThreads+ " threads.");
var Worker= require('webworker-threads');
var threadPool= Worker.createPool(numThreads).all.eval(fibo);

function tagg (req,res) {
  threadPool.any.eval('fibo(40)', function (err, data) {
    process.stdout.write('❚');
    res.end("TAGG -> "+ data); //Threads A GoGo
  });
}

http.createServer(fast).listen(12345), console.log("fast @ localhost:12345");
http.createServer(slow).listen(12346), console.log("slow @ localhost:12346");
http.createServer(tagg).listen(12347), console.log("useThreads @ localhost:12347");