admin管理员组

文章数量:1300006

I am trying to use web-workers or threads in my node application for the first time. I am using the webworker-threads npm module.

Basically I would like each worker to make requests to a server, measure the response time and send it back to the main thread.

I tried it many different ways, but I just can't seem to get it working. The basic examples from the docs work. But when I try to require a module ("request" in my case), the workers just seem to stop working, without any error messages. I saw in the docs that require doesn't work inside a worker, so I tried "importScripts()", which doesn't work either. When using threadpools I tried to use .all.eval() but it didn't work either.

Since this is the first time working with web-workers / threads in node, I might misunderstand how to use those things in general. Here is one example I tried:

server.js

var Worker = require('webworker-threads').Worker;
var worker = new Worker('worker.js');

worker.js

console.log("before import");
importScripts('./node_modules/request/request.js');
console.log("after import");

This basic example only prints before import and then stops.

I am trying to use web-workers or threads in my node application for the first time. I am using the webworker-threads npm module.

Basically I would like each worker to make requests to a server, measure the response time and send it back to the main thread.

I tried it many different ways, but I just can't seem to get it working. The basic examples from the docs work. But when I try to require a module ("request" in my case), the workers just seem to stop working, without any error messages. I saw in the docs that require doesn't work inside a worker, so I tried "importScripts()", which doesn't work either. When using threadpools I tried to use .all.eval() but it didn't work either.

Since this is the first time working with web-workers / threads in node, I might misunderstand how to use those things in general. Here is one example I tried:

server.js

var Worker = require('webworker-threads').Worker;
var worker = new Worker('worker.js');

worker.js

console.log("before import");
importScripts('./node_modules/request/request.js');
console.log("after import");

This basic example only prints before import and then stops.

Share Improve this question asked May 22, 2015 at 2:31 Andreas GassmannAndreas Gassmann 6,5447 gold badges34 silver badges47 bronze badges 2
  • 1 Out of curiosity, why make the http requests from a worker? (Requests made by request are asynchronous, so if you did make them from the main thread, they won't block it while waiting for a response) – Michal Charemza Commented May 24, 2015 at 7:24
  • I would like to make a small script to "stress test" one of my websites and generate a graph of the response times. And I noticed that you can make more requests by running them in parallel. I know, this is probably really inefficient, but it's basically just a small project to learn how threads work in node. I'm now using node's child_processes, and everything works fine. But I'm still not sure how I could do it with web workers (also, I'm not entirely sure what the difference between web workers and child processes is, or in what case to use which). – Andreas Gassmann Commented May 24, 2015 at 16:37
Add a ment  | 

1 Answer 1

Reset to default 6

Web workers are native javascript only so you can't achieve what you want with them. Worker threads don't support node.js api or npm packages(like http or request.js). For concurrency you don't need any multithread magic just use async.js or promises. If you want to play with threads then child_processes is the way to go. You could also use an API to manage child_processes like https://github./rvagg/node-worker-farm

Considering your example you could write something like this:

main.js

var workerFarm = require('worker-farm')
, workers    = workerFarm(require.resolve('./child'))
, ret        = 0;

var urls = ['https://www.google.', 'http://stackoverflow./', 'https://github./']; 

urls.forEach(function (url) {
    workers(url, function (err, res, body, responseTime) {
        console.log('Url ' + url + 'finished in ' + responseTime + 'ms');    
        //Ugly code here use async/promise instead
        if (++ret == urls.length)
            workerFarm.end(workers);
    });
});

child.js

var request = require('request');

module.exports = function(url, cb) {
    var start = new Date();
    request(url, function(err, res, body) {
        var responseTime = new Date() - start;
        cb(err, res, body, responseTime);
    });
};

本文标签: javascriptMake HTTP request inside Web WorkerStack Overflow