WorkerPool/multiple workers: what am I doing wrong here?

5 views
Skip to first unread message

Alex

unread,
Feb 22, 2011, 2:34:03 PM2/22/11
to Gears Users
I have some code that looks something like this:

function launchWorkerThread(inputObj, jsCode, jsWorkerFnName,
finishedCallbackFn) {
var workerPool = google.gears.factory.create('beta.workerpool');
workerPool.onmessage = function(a, b, message) {
finishedCallbackFn(message.body);
};
// Create JS code that calls "jsWorkerFnName" inside the user's
"jsCode" script
var workerCode = jsCode +
"var wp = google.gears.workerPool;\nwp.onmessage = function(a, b,
message){\n"+
"var replyObj=" + jsWorkerFnName + "(message.body.input);\n"+
"wp.sendMessage(replyObj, message.sender);\n}";

// Launch a worker and call user code
console.log("1");
var childWorkerId = workerPool.createWorker(workerCode);
console.log("2");
workerPool.sendMessage({input:inputObj}, childWorkerId);

// Debug code to demonstrate problem
console.log("1");
var childWorkerId2 = workerPool.createWorker(workerCode);
console.log("2");
workerPool.sendMessage({input:inputObj}, childWorkerId2);
console.log("1");
var childWorkerId3 = workerPool.createWorker(workerCode);
console.log("2");
workerPool.sendMessage({input:inputObj}, childWorkerId3);
console.log("1");
var childWorkerId4 = workerPool.createWorker(workerCode);
console.log("2");
workerPool.sendMessage({input:inputObj}, childWorkerId4);
}

When run without the "debug code", this works fine: ie it runs my
offload code (which hogs a CPU for 10s) while the UI thread remains
responsive. This is on Chrome 9.0.597.98 and I haven't touched the
default Gears installation.

However with the debug code running (or when the top level function is
called multiple times), the UI thread blocks on the second
createWorker call until the previous worker "onmessage" is complete
and then continues, and so on (as can be seen from the log messages).

Has anyone had a similar problem and/or can see what I'm doing wrong
or what the problem is?

Conversely if anyone has written code that does something similar,
which they know works, and is not the same as what I've done, could
they post that?

Thanks in advance

Alex
Reply all
Reply to author
Forward
0 new messages