The qualia:workers
package provides an easy way to parallelize code across multiple processes and fibers on the server. It's as easy as:
import Workers from 'meteor/qualia:workers';
let jobs = Workers.map([1, 2, 3], x => x*x);
console.log(Workers.waitAll(jobs));
// => [1, 4, 9]
$ meteor add qualia:workers
Using qualia:workers
is very simple. The usage shown in the introduction is really all there is to it. However, there are two very important things to keep in mind:
-
Workers run in their own processes, which means that there is no shared global state between them. It is a distinct instance of your application.
-
The function passed to
Workers.map
is stringified before being sent to a worker process. This means that it does not have access to the local scope in which it is declared (aka the closure is lost). So the following code would throw an error:
let hello = 'hello';
Workers.map(['yo', 'sup', 'hello'], greeting => {
return hello === greeting;
});
This fails because the variable hello
was declared outside of the function passed to Workers.map
.