andywer / andywer/threads.js

basic questions on threadsJs Pool

Aperta
#297 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
question
Lingua principale
TypeScript
Stelle
3.5k
Fork
173
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.