0

I am facing issue in accessing javascript function or any variable in worker thread. I am using WebWorker-thread nodejs library to perform multithreading.

var fs = require('fs');
var path = require('path');
var threadPool=Worker.createPool(5).all.eval(writeKeywordsToFile);

function getKeywords() {

    var keywords = ["Restaurant", "Food", "Cusine"];
    for (var i = 0; i < keywords.length; i++) {
        threadPool.any.eval('writeKeywordsToFile(' + JSON.stringify(keywords[i]) + ',' + i + ',' + JSON.stringify(path.join(__dirname, '../', 'tmp/')) + ')', function (err, val) {
            console.log(' [' + this.id + '] ---- >'  + err);                

        });
        //writeKeywordsToFile(keywords[i], i , path.join(__dirname, '../', 'tmp/'));
    }
}

function writeKeywordsToFile(keywords, i, dirPath) {


    if (keywords != undefined) {
        var fileName = "Document_" + i + ".txt";
        console.log(fileName);
        var stream = fs.createWriteStream(dirPath + fileName, fs);

        stream.once('open', function (fd) {
            console.log('stream open');

            stream.write(keywords + "\r\n");
            stream.end();
        });
    }
    return keywords;
}

getKeywords();

Unable to access fs to createstream and write to file. How can I access any javascript function or variable inside writeKeywordsToFile(), any workaround ?

1 Answer 1

1

Workers can only communicate with your original scope by sending/receiving messages to/from that scope. For all intents and purposes you should treat a thread as living in it's own separate scope. I will admit that my experience is based solely on working with actual web-workers, so I'm not sure in how far this applies to your node-based solution (although it should apply since multi-threading on shared scopes would invite hell and damnation on your code)

Thus, anything you've got on your global scope within the main thread simply does not exist in it's workers' scope...

You'll have to require('fs') in the worker if you want to do anything with it.

Sign up to request clarification or add additional context in comments.

2 Comments

but the problem is that I can't even access "require" keyword in the worker thread. Strange is that I can access console.write but not "require" :(
I dug around a little and found that the lib you're using, although attempting to come close to actual web-workers does miss out on a few features of said workers. There's indeed no 'require' available. Those workers are really only intended for doing computational heavy-lifting on a limited data-set and reporting the outcome back to the main thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.