javascript - Running multiple threads in parallel using Webworker-Threads for Node.js -
i new using node.js , webworker-threads node.js (https://github.com/audreyt/node-webworker-threads). webworker-threads module based on thread-a-gogo (https://github.com/xk/node-threads-a-gogo).
here summary of problem:
i have file called "readfile.js", contains code read csv file passed in, , converts csv data 2d array.
i want function ""exports.parsecsv" within "readfile.js" loaded , executed in multiple worker threads, each thread running in parallel.
i have file, "test1.js", attempts point 2 using thread pool:
var threads = require('webworker-threads'); var parser = require('./readfile.js'); var numthreads = 3; var threadpool = threads.createpool(numthreads); threadpool.all.eval(parser.parsecsv); (var = 1; <=3; i++) { (function(i) { threadpool.any.eval(parser.parsecsv("csvfile.csv"), function (err, val) { if (i == 3) { console.log("bye!"); threadpool.destroy(); } }); })(i); } - i have file, "test2.js", attempts point 2 manually creating threads:
function cb (err, result) { if (err) { console.log("\n" + " error! error! error!"); throw err; } console.log(" no error!"); thread.destroy(); } var threads = require('webworker-threads'); var parser = require('./readfile.js'); var thread = threads.create(); thread.eval(parser.parsecsv); thread.eval(parser.parsecsv("csvfile.csv"), cb); thread.eval(parser.parsecsv); thread.eval(parser.parsecsv("csvfile.csv"), cb); thread.eval(parser.parsecsv); thread.eval(parser.parsecsv("csvfile.csv"), cb); - when run "test[x].js" file using command
node test[x].jsin command prompt, threads seem working appears if being executed sequentially, , not in parallel. based on outputs of time each thread starts, print out command prompt window.
how execute multiple webworker-threads run in parallel in background? want threads start @ same time , end @ same (if possible). have looked @ api webworker-threads haven't been able solve problem. once works, use same methodology not read csv files, store contents database.
any appreciated! thank :)
Comments
Post a Comment