Skip to content

Instantly share code, notes, and snippets.

@oitee
Created February 9, 2022 14:01
Show Gist options
  • Save oitee/9e6615ebff2a4371914df92f59cbc6ba to your computer and use it in GitHub Desktop.
Save oitee/9e6615ebff2a4371914df92f59cbc6ba to your computer and use it in GitHub Desktop.
import * as nodeWorker from "worker_threads";
import * as db from "../src/db_connection.js";
import * as model from "../src/model.js";
db.poolStart();
let entityX = "X";
await model.insertEntity(entityX, "user");
const timeSlotX = {};
timeSlotX.from = Math.floor(
Date.parse(new Date("2021", "11", "15", "11", "00", "00")) / 1000
);
timeSlotX.to = Math.floor(
Date.parse(new Date("2021", "11", "15", "13", "00", "00")) / 1000
);
async function doSomething() {
console.log(`Trying to insert`);
await model.insertMeeting([entityX], timeSlotX.from, timeSlotX.to)
.then(res => console.log(res));
}
if (nodeWorker.isMainThread) {
console.log("master: process id is " + process.pid);
const threads = new Set();
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
threads.add(new nodeWorker.Worker("./worker_demo.js"));
for (let worker of threads) {
worker.on("exit", () => {
threads.delete(worker);
console.log(`master: Thread exiting, ${threads.size} still running...`);
});
}
for (let worker of threads) {
worker.postMessage("go");
}
} else {
nodeWorker.parentPort.on("message", async (msg) => {
await doSomething();
process.exit();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment