parallelism and thread with javascript

Viewed 52

If I'm trying to make a simple parallelism implementation in javascript:

  • 1 main programme, which will create the different thread.
  • 1 thread, which will read a .txt file, and which will push each word into an array.
  • 1 thread, which will read the array and print the first element + delete the first element if the array is not empty.

Last thing I try :

main.js:

const { fork } = require('child_process');
var fs = require('fs');

const forked = fork('child.js');

const openFile = async (text) => {
    fs.readFile(text, async function read(err, data) {
        if (err) {
            console.log("impossible a lire")
        }
        else{
            console.log("début lecture du fichier")
            data = data.toString().split(" ");
            for(var i = 0; i<data.length; i++){
                console.log("mot dans parent = " + data[i])
                forked.send({ mot: data[i] });
            }

        }
    })
}

openFile('input.txt')

Child.js:

process.on('message', (msg) => {
    console.log(msg);
  });

But this look like to be concurrency and not parallelism.

Any idea?

0 Answers
Related