async
async copied to clipboard
Multicore
I tried to search in closed topics but found nothing. Does this lib support multicore?
Imagine I have an array with 1 million filenames: ['1.txt', '2.txt', etc., ..., '1000000.txt'] and I need to do heavy processing and then write the result in the respective file?
What would be the method to efficiently use all the cores of the CPU to spread the CPU intensive processing towards different cores among different filenames?
Normally I would use this:
const fs = require('fs')
const fs = require('async')
const heavyProcessing = require('./heavyProcessing.js')
const files = ['1.txt', '2.txt', ..., '1000000.txt']
async.each(files, function (file, cb) {
fs.writeFile(file, heavyProcessing(file), function (err) {
if (err) cb(Error(err)) else cb()
})
})
I guess the heavyProcessing function is always run within the same thread. Am I right? Can I spawn it towards different cores/threads to improve overall speed?
Just for everyone to know, if they are interested, you need to use the npm module piscina.
In this gist I explain everything.
This library is about managing the cooperative multi-tasking within a single JS thread. It doesn't handle multi-threading or multi-core.