andywer / andywer/threads.js

basic questions on threadsJs Pool

Offen
#297 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
question
Vorherrschende Sprache
TypeScript
Sterne
3.5k
Forks
173
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.