Skip to content

Instantly share code, notes, and snippets.

@caesaneer
Created August 11, 2019 18:59
Show Gist options
  • Save caesaneer/2b8a8b2ea2448ab00e1e82ab9a15b327 to your computer and use it in GitHub Desktop.
Save caesaneer/2b8a8b2ea2448ab00e1e82ab9a15b327 to your computer and use it in GitHub Desktop.
// ════════════════════════════════════════════════════════════════════════════════════════════════╡
const { parentPort, workerData } = require('worker_threads')
const crypto = require('crypto')
const util = require('util')
const fs = require('fs')
// ════════════════════════════════════════════════════════════════════════════════════════════════╡
const asyncFs = util.promisify(fs.readFile)
parentPort.on('message', async (times) => {
if (typeof times !== 'number') {
throw new Error('times must be a number.')
}
const result = await asyncFs('./data/test.dat', 'utf8')
let resStr = ''
for (let i = 0; i < times; i += 1) {
const x = crypto.createHash('sha256')
x.update(result, 'utf8')
resStr = x.digest('hex')
}
// Access the workerData.
// console.log('workerData is', workerData)
// return the result to main thread.
parentPort.postMessage(resStr)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment