task.js
This modules is intended to make working with blocking tasks a bit easier, and is meant to work in node as well as the browser.
This branch uses the webworker-threads module instead of forking
install
# node
npm install task.js_threads
or
# browser
bower install task
or just grab the cdnjs hosted version directly
important
Before using this module I want to expose the current performance issues
-
Node.js
- When using task.js in node with very large messages it can be very slow, this is due to the fact that node is copying all the message data.
-
Clientside
- When using task.js in a browser that doesn't support transferables (or you don't use them properly) then you will notice a slow down when passing massive array buffers
Rule of thumb in node keep your object size under 150kb, and in the clientside version you can go crazy and send 40MB array buffers if transferables are supported.
callbacks and promises
task.js supports both styles
var powTask = task; // callbacks; // promises;
task.defaults (optional)
You can override the defaults like this
// overriding defaults (optional)var myCustomTask = task;
behind the scenes it's spreading your dynamic work across your cores
task.wrap
You can wrap a function if the method signatures match, and it doesn't rely on any external variables.
// non async { return Math;} var powTask = task;;
But keep in mind that your function cannot reference anything inside of your current scope because it is running inside of a worker.
task.run
Below is an example of using a transferable
var buffer = ; task;
task.terminate
When you run terminate it will destroy all current workers in the pool, and throw an error on all outstanding work.
task;
globals in workers
You can initialize a task instance to have predefined data from your main thread, or generated within the worker.
var data = one: 1; task task;
The above works great for small data, but with larger data this doesn't work. This is where you can use the initialize property in defaults.
task;
You can also use initialize to define common methods in the worker scope as well.
task;
Keep in mind that it is ok to have a slow initialize, no work will actually be processed until there is a fully initialized worker.
task.setGlobals
You can set globals across all of your workers like this
task;
The above will kill all current workers, create new ones, and then assign the new workers the outstanding tasks.