forked from uNetworking/uWebSockets.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkerThreads.js
33 lines (30 loc) · 1.01 KB
/
WorkerThreads.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
/* This example spawns two worker threads, each with their own
* server listening to the same port (Linux feature). */
const uWS = require('../dist/uws.js');
const port = 9001;
const { Worker, isMainThread, threadId } = require('worker_threads');
const os = require('os');
if (isMainThread) {
/* Main thread loops over all CPUs */
/* In this case we only spawn two (hardcoded) */
/*os.cpus()*/[0, 1].forEach(() => {
/* Spawn a new thread running this source file */
new Worker(__filename);
});
/* I guess main thread joins by default? */
} else {
/* Here we are inside a worker thread */
const app = uWS.SSLApp({
key_file_name: 'misc/key.pem',
cert_file_name: 'misc/cert.pem',
passphrase: '1234'
}).get('/*', (res, req) => {
res.end('Hello Worker!');
}).listen(port, (token) => {
if (token) {
console.log('Listening to port ' + port + ' from thread ' + threadId);
} else {
console.log('Failed to listen to port ' + port + ' from thread ' + threadId);
}
});
}