andywer / andywer/threads.js

basic questions on threadsJs Pool

未关闭
#297 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
question
主要语言
TypeScript
星标
3.5k
派生
173
PR 合并指标
30 天内没有已合并 PR

描述

Hi,
I am just a beginner to NodeJs and also ThreadJs. I was trying out few examples related to ThreadJS pool just to understand the concepts. I came up with below example for main.js and worker.js to kind of measure how NodeJs behaves with different pool size by measuring the execution time:
---main.js---
```
const threads = require( 'threads' )

async function main(){
const pool = threads.Pool( () => threads.spawn( new threads.Worker( "sample" ) ), 2 )

pool.queue( forFromN => forFromN( 1 ) );// task 1
pool.queue( forFromN => forFromN( 2 ) );//task 2

for( var tCount = 3; tCount <= 4; tCount++ ){//task 3 & 4
pool.queue( forFromN => forFromN( tCount ) );
}

console.log( "Start time: " + new Date().toString() );
var startTime = new Date().getTime();

await pool.completed()
await pool.terminate()

var endTime = new Date().getTime();
console.log( "End time: " + new Date().toString() );

console.log( "Time taken: " + ( endTime - startTime ) + "(ms)")
}

main()
```
---worker.js---
```
const expose = require( "threads/worker" )
expose.expose( function( n ){
console.log( "n :" + n );
var iLength = n * 10000;
for( var iCount = 1; iCount <= iLength; iCount++ )
{
for( var jCount = 1; jCount <= 1000000; jCount++ )
{
f1( jCount );
}
}
} );

function f1( i ){
return i * 1;
}
```
Now, below are my questions related to above code:
1. in this for loop i was hoping that tCount will passed as 3 and 4 but looks like when the code executes it picks up tCount as 5 for both 3rd and 4th task. Is this the expected behaviour or am I missing something. And if i add await before pool.queue( forFromN => forFromN( tCount ) ); it starts the execution of pool tasks which i don't want to do at that point in time.
```
for( var tCount = 3; tCount <= 4; tCount++ ){//task 3 & 4
pool.queue( forFromN => forFromN( tCount ) );
}
```
2. suppose if I want to pass huge chunk of JSON objects to the task, is there a way to better way to pass the data?

thanks in advance.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。