Features
- Run multipe tasks parallely without blocking the main thread.
- Configure task(set priority, time-to-live) and Priority Task Queue Management System(PTQMS) will take care of the queue handling and execution.
- On-demand initialization of web-workers based on task-queue size. Configure minimum and maximum number of web-workers or let library smartly decide based on hardware concurrency number.
- Termination of idle(non-used) web-workers for better performance(can be opt-out while configuring). Set minimum number of web-workers to restrict their termination when idle, so that they remain present for future needs.
- Fully fledged API to get all the information regarding tasks and workers at any given point of time.
- Promise based execution of tasks i.e. perform different actions on success or failure. Useful for re-attempting failed task.
- Define callbacks to be get triggered at various events. Eg. on successful execution of task, on exceeding the time limit of task execution, etc.
- Exports in a UMD format i.e. library works everywhere.
Flow diagram
Explanation of diagram
Setting the workers configuration
Lets have the following worker configuration:Task Queue Process Management
Web worker Execution
i) All workers are busy i.e. maxWorkers number has been reached, so, tasks need to be in queue for more time before they can be picked up for execution.
ii) No worker has been spwaned yet and maxNumbers is greater than the number of spawned workers.
API
Create an instance / reference before using.var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js'
});
Contructor accepts a configurable Object.
Config keys | Default | Accepts |
---|---|---|
url | - | path of worker script |
maxWorkers | Depends on CPU cores(hardwareConcurrency) | Number |
minWorkers | 1 | Number |
killWorkersAfterJob | true | Boolean |
Examples:
Url of the worker script is mandatoryvar sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js'
});
Setting maxWorkers to restrict the spawning of web-workers after a limit
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js',
maxWorkers: 3
});
Setting minWorkers so that we always have that many web-workers always ready to work
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js',
minWorkers: 2
});
Configuring setting so that idle workers would be terminated once they are done with thier job. One worker will remain always there even if it's idle.
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js',
killWorkersAfterJob: true
});
Main thread Methods
exec
Executes a task based on the task configuration. Following types of JavaScript objects are supported:- Function
- String
Parameter | Description |
---|---|
function/string | Function which needs to be executes(either onload or offload a method) |
params | Array of params the task-function requires |
task config | Setting priority, name, etc. of a task. |
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js'
});
// This method is not isnide the worker-script
// "Onloading of method
function add (a, b) {
return a + b;
}
sp.exec(add, [1,2], {
priority: 'low',
name: 'add-me'
}).then(function (val) {
console.log(val);
}).catch(function (err) {
console.log(err)
});
// ________my-worker.js____________
// This method SHOULD be inside the worker-script
// "Offloading of method"
/* function add (a, b) {
return a + b;
} */
// _________index.js________________
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js'
});
sp.exec('add', [1,2], {
priority: 'low',
name: 'add-me'
}).then(function (val) {
console.log(val);
}).catch(function (err) {
console.log(err)
});
getAllWorkers
Returns the list of all the workers.sp.getAllWorkers();
getActiveWorkers
Returns the list of all active workers.sp.getActiveWorkers();
getIdleWorkers
Returns the list of all idle workers.sp.getIdleWorkers();
getTerminatedWorkers
Returns the list of all the terminated workers.sp.getTerminatedWorkers();
terminateAllWorkers
Termiates all the workers irrespective of the status.sp.terminateAllWorkers();
terminateWorker
Terminated a particular worker by passing the worker-id.Parameter | Description |
---|---|
id | id of the worker to be terminated |
sp.terminateWorker('57cd47da-d98e-4a2d-814c-9b07cb51059c');
broadCastAll
Sends a same message to all the active workers (Message would be sent via postMessage API)Parameter | Description |
---|---|
id | id of the worker to be terminated |
sp.broadCastAll('Hello my dear Child! A greeting from Parent.'});
broadCastTo
Sends a message to a particular active worker (Message would be sent via postMessage API)Parameter | Description |
---|---|
id | id of the worker to be terminated |
msg | msg to be sent |
sp.broadCastTo('57cd47da-d98e-4a2d-814c-9b07cb51059c', 'Hey! Can you run the script: worker.js? Thanks!');
Child(Worker) thread Methods
sendMessage
Sends a message to Main thread.Parameter | Description | Accepts |
---|---|---|
msg | msg to be sent | Object |
origin | origin | String |
child.sendMessage(obj, '*');
exposeMethods
It works as a proxy. Methods which needs to be onloaded should be exposed before they can used in main thread.Parameter | Description | Accepts |
---|---|---|
methods | list of all functions | Object |
child.exposeMethods({
add: function (a, b) {
return a + b;
}
});
Task Queue Methods
get
Returns the task.Parameter | Description |
---|---|
id | id of the task |
sp.taskQueue.get('34cd47da-d98e-4a2d-814c-9b07cb510578');
getAll
Returns the list of all tasks.sp.getAll();
getActive
Returns the list of all active tasks.sp.getActive();
getCompleted
Returns the list of all completed tasks.sp.getCompleted();
getFailed
Returns the list of all the failed tasks.sp.getFailed();
tasks and allTasks
Returns tasks which are in queue.sp.taskQueue.tasks
Returns all the tasks i.e. pending, queued, active and completed/failed.
sp.taskQueue.allTasks
Usage
Instantiating the library with config
Example 1: With default settings
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js'
});
Example 2: Restricting min and max number of workers manually
// `maxWorkers` depends on CPU cores.
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js',
maxWorkers: 3,
minWorkers: 1
});
Example 3: Terminating workers if idle
var sp = new SuperWorkers.MainThread({
url: 'js/my-worker.js',
maxWorkers: 3, // hardwareConcurrency
minWorkers: 1,
killWorkersAfterJob: true
});
Executing tasks
Example 1: Execute a task defined outside of worker script(Offloading a function)
function add(a, b) {
return a + b;
}
sp.exec(add, [1,2], {
priority: 'low',
name: 'run-add'
}).then(function (val) {
console.log(val);
}).catch(function (err) {
console.log(err)
});
Example 2: Execute a task defined inside of worker script and of somewhat medium priority(Onlading a function)
sp.exec('add', [1,2], {
priority: 'medium',
name: 'run-add-as-medium'
}).then(function (val) {
console.log(val);
}).catch(function (err) {
console.log(err)
});
Example 3: Execute a task with utmost(high) priority
function fibonacci(n) {
return n < 1 ? 0
: n <= 2 ? 1
: fibonacci(n - 1) + fibonacci(n - 2);
}
sp.exec(fibonacci, [5], {
priority: 'high',
name: 'run-fibonacci-as-soon-as'
}).then(function (val) {
console.log(val);
}).catch(function (err) {
console.log(err)
});
Live Demo
Workers Configuration
Input is not a number!
Input is not a number!
Available Tasks
{{ task.name }} - {{ task.label }}
of {{ task.priority }} priority
should output {{ task.output }}.
of {{ task.priority }} priority
should output {{ task.output }}.
Please click on above tasks.
Some points to remember:
Task Queue
{{ task.status === 'completed' ? 'check_circle' : task.status === 'queued' ? 'queue' : task.status === 'failed' ? 'error_outline' : '' }}
{{ task.name }}
{{ task.name }}
Name
{{ task.priority }} Priority
{{ task.status }} Status
{{ task.id }} Id
{{ task.runningOnWorkerId }} Running on worker
{{ task.priority }} Priority
{{ task.status }} Status
{{ task.id }} Id
{{ task.runningOnWorkerId }} Running on worker
Tasks Info
Task {{task.name}} is {{ task.status }}
Output - {{ task.output }}
Workers Info
{{ worker.id }} -
{{ worker.status }}
Tasks running on Workers
Task Id: {{ task.id }}
Name: {{ task.name }}
Priority: {{ task.priority }}
RunningOnWorker: {{ task.runningOnWorkerId || '...' }}
Status: {{ task.status }}