From d5fdf36f77161690cbf5fc65c44931bc44000a20 Mon Sep 17 00:00:00 2001 From: hgw Date: Mon, 2 Oct 2023 02:14:36 +0000 Subject: [PATCH] godwords multithreading --- bot.js | 25 +++++++++++++++++-------- wordgen.js | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 wordgen.js diff --git a/bot.js b/bot.js index 09759ab..251b198 100644 --- a/bot.js +++ b/bot.js @@ -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(" ") - } - bot.say(chan, string); + //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); + }); } } diff --git a/wordgen.js b/wordgen.js new file mode 100644 index 0000000..921250c --- /dev/null +++ b/wordgen.js @@ -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() \ No newline at end of file