使用 worker_threads 处理多维数组
Processing a multidimensional array with worker_threads
我有一个有趣的任务,但我什至在学习工人方面感到困惑。有一个 10-30K 对象的维度数组。我想在可用流的数量和每个子数组中将其分解成子数组,以实现在某些字段中搜索所需对象的功能。
关于将数组划分为子数组的问题以及搜索功能的实现 - 一切都很好。但是如何在每个子阵列中同时在工人的帮助下开始搜索 - 有麻烦(
刚开始接触工友,还不是很了解。如果能提供帮助或建议,我将不胜感激。
P.S。执行代码时出现错误
function evalInWorker(f){
if (isMainThread){
return new Promise((res, rej) =>{
const worker = new Worker(__filename, {eval: true});
worker.on('error', e => rej(e));
worker.on('message', msg => {
res(msg);
});
worker.on('exit', code => {
if(code !== 0)
rej(new Error(`Worker stopped with exit code ${code}`));
});
});
}else {
parentPort.postMessage(f());
}
}
//getSlicedArr возвращает массив с подмассивами, search - ищет в подмассиве объект по нужным свойствам needToFind
const tasks = (threads, dataArr, needToFind, arr = \[\]) => {
getSlicedArr(dataArr, threads).map( e => arr.push(evalInWorker(search(e, needToFind))));
return arr;
};
Promise.all(tasks(subArrSize, dataArr, needToFind))
.then(messList => {
messList.forEach(m => console.log(m))
})
.catch(e => console.log(e));
出于这种目的,您可以查看 microjob 专门为此类内容构建的库。
这是您的上下文示例(它使用 Typescript,但与 JS 相同:
import { start, stop, job } from 'microjob';
const main = async () => {
await start();
// build an array of 5 arrays filled with random numbers
const table = Array.from({ length: 5 }).map(_ => Array.from({ length: 100 }).map(Math.random));
// this is your search function (just a placeholder here)
const search = (arr: number[]): number => {
console.log(arr);
return arr[0];
};
// job executes the search function inside a new thread
// so the executions are made in parallel
const res = await Promise.all(table.map(data => job(search, { data })));
console.log(res);
await stop();
};
main();
我有一个有趣的任务,但我什至在学习工人方面感到困惑。有一个 10-30K 对象的维度数组。我想在可用流的数量和每个子数组中将其分解成子数组,以实现在某些字段中搜索所需对象的功能。
关于将数组划分为子数组的问题以及搜索功能的实现 - 一切都很好。但是如何在每个子阵列中同时在工人的帮助下开始搜索 - 有麻烦(
刚开始接触工友,还不是很了解。如果能提供帮助或建议,我将不胜感激。
P.S。执行代码时出现错误
function evalInWorker(f){
if (isMainThread){
return new Promise((res, rej) =>{
const worker = new Worker(__filename, {eval: true});
worker.on('error', e => rej(e));
worker.on('message', msg => {
res(msg);
});
worker.on('exit', code => {
if(code !== 0)
rej(new Error(`Worker stopped with exit code ${code}`));
});
});
}else {
parentPort.postMessage(f());
}
}
//getSlicedArr возвращает массив с подмассивами, search - ищет в подмассиве объект по нужным свойствам needToFind
const tasks = (threads, dataArr, needToFind, arr = \[\]) => {
getSlicedArr(dataArr, threads).map( e => arr.push(evalInWorker(search(e, needToFind))));
return arr;
};
Promise.all(tasks(subArrSize, dataArr, needToFind))
.then(messList => {
messList.forEach(m => console.log(m))
})
.catch(e => console.log(e));
出于这种目的,您可以查看 microjob 专门为此类内容构建的库。
这是您的上下文示例(它使用 Typescript,但与 JS 相同:
import { start, stop, job } from 'microjob';
const main = async () => {
await start();
// build an array of 5 arrays filled with random numbers
const table = Array.from({ length: 5 }).map(_ => Array.from({ length: 100 }).map(Math.random));
// this is your search function (just a placeholder here)
const search = (arr: number[]): number => {
console.log(arr);
return arr[0];
};
// job executes the search function inside a new thread
// so the executions are made in parallel
const res = await Promise.all(table.map(data => job(search, { data })));
console.log(res);
await stop();
};
main();