Skip to content

Commit 78d489c

Browse files
committed
Refactor examples
1 parent 40fcf81 commit 78d489c

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
Запускаем 'node multicore', который подгружает сначала `master.js` и происходит
1313
порождение дочерних процессов по количеству ядер процессора
14-
`api.os.cpus().length` при помощи `api.cluster.fork()`, ссылки на воркеры
14+
`os.cpus().length` при помощи `cluster.fork()`, ссылки на воркеры
1515
складываются в массив `workers`. Родительский процесс рассылает дочерним через
1616
`worker.send()` задачу на исполнение. Дочерний процесс ловит задачу (это
1717
массив) и перемножает все его элементы на 2, после чего отправляет результат

tcp/client.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict';
22

3-
global.api = {};
4-
api.net = require('net');
3+
const net = require('net');
54

6-
const socket = new api.net.Socket();
5+
const socket = new net.Socket();
76

87
let user;
98

@@ -14,7 +13,7 @@ socket.connect({
1413
socket.write('Hello from client');
1514
socket.on('data', (data) => {
1615
user = JSON.parse(data);
17-
console.log('Data received (by client): ' + data);
18-
console.log('Age of ' + user.name + ' is ' + user.age);
16+
console.log(`Data received (by client): ${data}`);
17+
console.log(`Age of ${user.name} is ${user.age}`);
1918
});
2019
});

tcp/server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
22

3-
global.api = {};
4-
api.net = require('net');
3+
const net = require('net');
54

65
const user = { name: 'Marcus Aurelius', age: 1895 };
76

8-
const server = api.net.createServer((socket) => {
7+
const server = net.createServer((socket) => {
98
console.log('Connected: ' + socket.localAddress);
109
socket.write(JSON.stringify(user));
1110
socket.on('data', (data) => {

workers/master.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use strict';
22

3+
const os = require('os');
4+
const cluster = require('cluster');
5+
36
module.exports = () => {
47

5-
const cpuCount = api.os.cpus().length;
8+
const cpuCount = os.cpus().length;
69

710
const workers = [];
811
for (let i = 0; i < cpuCount; i++) {
9-
const worker = api.cluster.fork();
12+
const worker = cluster.fork();
1013
workers.push(worker);
1114
}
1215

@@ -24,8 +27,8 @@ module.exports = () => {
2427
worker.on('message', (message) => {
2528

2629
console.log(
27-
'message from worker ' + worker.process.pid + ': ' +
28-
JSON.stringify(message)
30+
'message from worker ' + worker.process.pid +
31+
': ' + JSON.stringify(message)
2932
);
3033
results.push(message.result);
3134

workers/multicore.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict';
22

3-
global.api = {};
4-
api.cluster = require('cluster');
5-
api.os = require('os');
3+
const cluster = require('cluster');
64

7-
if (api.cluster.isMaster) {
5+
if (cluster.isMaster) {
86
require('./master.js')();
97
} else {
108
require('./worker.js')();

workers/worker.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
'use strict';
22

3+
const cluster = require('cluster');
4+
35
module.exports = () => {
46

5-
console.log('Hello from worker ' + process.pid + ' ' + api.cluster.worker.id);
7+
console.log(
8+
'Hello from worker ' + process.pid +
9+
' ' + cluster.worker.id
10+
);
611

712
const caltulations = item => item * 2;
813

0 commit comments

Comments
 (0)