1
votes

I have a piece of code in node js that takes a few seconds the finish so i started using threads_a_gogo module( https://github.com/xk/node-threads-a-gogo ) to not block http requests. I spent a lot of times trying to figure out why the threat spawn is not working because my requests are still blocked.

app.post('/results.html', function(req, res){


    if( config.debug )
        console.log("thread created");
    var t = threads.create();
    var r = new rounds();
    t.eval( r.start(req,res), function(err, result) {

        r.finish( res );
        //t.destroy();
        if( config.debug )
            console.log("thread destroyed");
    });
    console.log("request finished");



});

console.log("request finished"); is only displayed AFTER the new thread finish but it is suppose to show almost when the request is done and its blocking request to the server until the process finish.

Any idea about what im doing wrong?

Thx

1
What are you trying to accomplish? Perhaps there is an easier/better way than using this module and methodJamis Charles
@ Jamis Charles r.start takes always a few seconds to answer and thats why i want to put it in a new thread. In theory this module should put that "task" in background and allow other request to arrive instead of blocking the main node thread. I want to use threads and not process so "cluster" module is not useful for me. And no, i can't touch the r object to make it faster and not use threads.nathanr
if you need threads, don't use node.jsvkurchatkin

1 Answers

2
votes

You are actually executing r.start(req,res) in the main thread, and then passing the return value to t.eval. eval accepts a string, which is then executed in the threads global scope. Something like this should work:

t.eval('var start =' + r.start.toString()).eval('start()'), function(err, result) { });