-
Notifications
You must be signed in to change notification settings - Fork 106
/
index.js
60 lines (48 loc) · 1.59 KB
/
index.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
const spq = require('shuffled-priority-queue');
const Piscina = require('../..');
const { resolve } = require('path');
// By default, Piscina uses a simple Fifo array task queue.
// This example replaces the default task queue with a
// priority queue.
// When a task is submitted to the pool, if there are workers
// available it will be dispatched immediately, regardless of
// the priority. The task queue is only used if there are no
// free workers
const kItem = Symbol('item');
class PriorityTaskQueue {
queue = spq();
get size () { return this.queue.length; }
push (value) {
const queueOptions = value[Piscina.queueOptionsSymbol];
const priority = queueOptions ? (queueOptions.priority || 0) : 0;
value[kItem] = this.queue.add({ priority, value });
}
remove (value) {
this.queue.remove(value[kItem]);
}
shift () {
return this.queue.shift().value;
}
}
const pool = new Piscina({
filename: resolve(__dirname, 'worker.js'),
taskQueue: new PriorityTaskQueue(),
maxThreads: 4
});
function makeTask (task, priority) {
return { ...task, [Piscina.queueOptionsSymbol]: { priority } };
}
(async () => {
// Submit enough tasks to ensure that at least some are queued
console.log(await Promise.all([
pool.run(makeTask({ priority: 1 }, 1)),
pool.run(makeTask({ priority: 2 }, 2)),
pool.run(makeTask({ priority: 3 }, 3)),
pool.run(makeTask({ priority: 4 }, 4)),
pool.run(makeTask({ priority: 5 }, 5)),
pool.run(makeTask({ priority: 6 }, 6)),
pool.run(makeTask({ priority: 7 }, 7)),
pool.run(makeTask({ priority: 8 }, 8))
]));
})();