andywer / andywer/threads.js

basic questions on threadsJs Pool

Ouverte
#297 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
question
Langage dominant
TypeScript
Étoiles
3.5k
Forks
173
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.