-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (29 loc) · 1012 Bytes
/
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
import http from "node:http";
import { Worker } from "worker_threads";
import path from "path";
import { fileURLToPath } from "url";
const currentDir = path.dirname(fileURLToPath(import.meta.url));
const mainContext = {
id: 1,
name: "amit",
};
http
.createServer((req, res) => {
console.log("mainContext in the beginning: ", mainContext);
const thread1 = new Worker(path.resolve(currentDir, "thread-1.js"));
const thread2 = new Worker(path.resolve(currentDir, "thread-2.js"));
thread1.on("message", (context) => {
Object.assign(mainContext, context);
console.log("mainContext after thread-1: ", mainContext);
});
thread2.on("message", (context) => {
Object.assign(mainContext, context);
console.log("mainContext after thread-2: ", mainContext);
});
thread1.postMessage(mainContext);
thread2.postMessage(mainContext);
res.end("Hello World!");
})
.listen(3333);
http.get("http://localhost:3333");
http.get("http://localhost:3333");