forked from GitSquared/edex-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_multithread.js
More file actions
86 lines (71 loc) · 2.22 KB
/
Copy path_multithread.js
File metadata and controls
86 lines (71 loc) · 2.22 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const cluster = require("cluster");
if (cluster.isMaster) {
const electron = require("electron");
const ipc = electron.ipcMain;
const signale = require("signale");
const numCPUs = require("os").cpus().length - 1; // Leave a core available for the renderer process
const si = require("systeminformation");
cluster.setupMaster({
exec: require("path").join(__dirname, "_multithread.js")
});
let workers = [];
cluster.on("fork", worker => {
workers.push(worker.id);
});
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
signale.success("Multithreaded controller ready");
var lastID = 0;
function dispatch(type, id, arg) {
let selectedID = lastID+1;
if (selectedID > numCPUs-1) selectedID = 0;
cluster.workers[workers[selectedID]].send(JSON.stringify({
id,
type,
arg
}));
lastID = selectedID;
}
var queue = {};
ipc.on("systeminformation-call", (e, type, id, ...args) => {
if (!si[type]) {
signale.warn("Illegal request for systeminformation");
return;
}
if (args.length > 1) {
si[type](...args).then(res => {
if (e.sender) {
e.sender.send("systeminformation-reply-"+id, res);
}
});
} else {
queue[id] = e.sender;
dispatch(type, id, args[0]);
}
});
cluster.on("message", (worker, msg) => {
msg = JSON.parse(msg);
try {
if (!queue[msg.id].isDestroyed()) {
queue[msg.id].send("systeminformation-reply-"+msg.id, msg.res);
delete queue[msg.id];
}
} catch(e) {
// Window has been closed, ignore.
}
});
} else if (cluster.isWorker) {
const signale = require("signale");
const si = require("systeminformation");
signale.info("Multithread worker started at "+process.pid);
process.on("message", msg => {
msg = JSON.parse(msg);
si[msg.type](msg.arg).then(res => {
process.send(JSON.stringify({
id: msg.id,
res
}));
});
});
}