andywer / andywer/threads.js

basic questions on threadsJs Pool

オープン
#297 コメント 0 件 リアクション 0 件 担当者 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 を短くまとめたダイジェスト。