-
Notifications
You must be signed in to change notification settings - Fork 0
/
workerlogic.js
60 lines (53 loc) · 1.4 KB
/
workerlogic.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
const Channel = require("channel");
const co = require("co");
class Pack {
constructor(size = 2, async = false) {
this.in = new Channel(1000);
for (let i = 0; i < size; i++) {
if (async)
co(function* () {
while (1) {
const cmd = yield this.in.recv();
if (!cmd)
break;
if (cmd == "stop") {
this.in.close();
continue;
}
let w = new Worker(cmd);
yield w.a()
}
}.bind(this))
else
co(function* () {
while (1) {
const cmd = yield this.in.recv();
if (!cmd)
break;
if (cmd == "stop")
this.in.close();
let w = new Worker(cmd);
w.s()
}
}.bind(this))
}
}
feed(cmd) {
this.in.send(cmd)
}
cork() {
this.in.send("stop")
}
}
class Worker {
constructor(cmd) {
this.cmd = cmd;
}
s() {
this.cmd()
}
async a() {
await this.cmd()
}
}
module.exports = {Worker, Pack};