godwords multithreading

This commit is contained in:
hgw 2023-10-02 02:14:36 +00:00
parent 22005153d0
commit d5fdf36f77
2 changed files with 35 additions and 8 deletions

21
bot.js
View File

@ -3,7 +3,8 @@ var fs = require("fs");
var readline = require('readline');
var path = require('path');
var randomext = require('./random');
var randomWords = require('better-random-words');
const { Worker } = require('worker_threads');
//var randomWords = require('better-random-words');
var config = { //edit your shit here
server: "irc.supernets.org",
@ -156,16 +157,24 @@ async function godwords(chan, amt) {
if (amt > 100000) {
bot.say(chan, "no")
} else {
text = [];
if (amt === undefined) {
var amt = 50
}
for(var i=0; i < amt; i++){
var word = randomWords({ exactly: 1 });
text.push(word);
var string = text.join(" ")
//for(var i=0; i < amt; i++){
// var word = randomWords({ exactly: 1 });
// text.push(word);
// var string = text.join(" ")
//}
const worker = new Worker('./wordgen.js', {
workerData: {
amt
}
});
worker.once('message', (string) => {
console.log('Received the hashedArray from the worker thread!');
bot.say(chan, string);
});
}
}

18
wordgen.js Normal file
View File

@ -0,0 +1,18 @@
const { parentPort, workerData } = require('worker_threads');
const { amt } = workerData;
var randomWords = require('better-random-words');
var hashedArray = [];
var string = [];
var text = [];
for(var i=0; i < amt; i++){
var word = randomWords({ exactly: 1 });
text.push(word);
}
var string = text.join(" ")
hashedArray.push(string);
//console.log(string);
// send the hashedArray to the parent thread
parentPort.postMessage(hashedArray);
process.exit()