Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

javascript - Does a Firefox Workers limit exist?

Im trying to create web Workers and post messages to them in cycle:

array.forEach(function (data) {
        this.createWorker();
        this.workers[this.workersPointer].postMessage({task: 'someTask', data: string});
    }, this);

createWorker function:

createWorker: function () {
    this.workersPointer++;
    var worker = this.workers[this.workersPointer] = new Worker('Worker.js'),
        storage = this;
    worker.onmessage = function (event) {
        if (event.data.error) {
            storage[event.data.task + 'Errback'](event.data.error);
        }
        else {
            storage[event.data.task + 'Callback'](event.data.data);
        }
    };
    worker.onerror = function (error) {
        storage.workerErrback(error);
    };
}

Worker code:

self.addEventListener('message', function (event) {
self.postMessage({
    data: data,
    error: err,
    task: event.data.task
});

}, false);

It works perfectly in Google Chrome. When I'm trying to run it in Firefox, it works only 20 times. Do Firefox web workers have a limit? I can't find information about it on mozilla.org. If there is no limit, what's the problem? Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just did some test of my own. For this, i changed the code a little bit:

Cycle:

for(var i=0;i<200;i++){
   this.createWorker();
   this.workers[this.workersPointer].postMessage({task: 'someTask', number:i});
};

createWorker function:

this.workers =[];
this.workersPointer = 0;
storage=[];


var createWorker= function () {
    workersPointer++;
    var myPointer = workersPointer;
    var worker = this.workers[this.workersPointer] = new Worker('Worker.js');

    worker.onmessage = function (event) {
        if (event.data.error) {
            alert(event.data.error);
        }
        else {
            document.cookie=event.data.task+"["+myPointer+"]="+event.data.number;
        }
    };
    worker.onerror = function (event) {
        alert("Error: " + event.error);
    };
}

Worker:

onmessage = function(event) {
    postMessage({number:event.data.number*2, task: event.data.task});
};

After i run this, in chrome i got 66 cookies (including a nice blue crash window), in firefox i got 20. So both browsers seem to have worker limitations.

EDIT:

In Opera i get a console message:

Maximum number of Web Worker instances(16) exceeded for this window.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...