Socket
Socket
Sign inDemoInstall

simple-tasks

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    simple-tasks

Easy way to dispatch your functions to a separate process making them truly async


Version published
Weekly downloads
3
Maintainers
1
Created
Weekly downloads
 

Readme

Source

node-simple-tasks

Node.js module for executing functions in a separate process. Simple design depends only on Node.js core ensuring high compatibility with future versions.

Importing the module with require() creates a new worker process and returns it's task queue. Tasks are executed sequentially in same order they were scheduled. If you need more work queues, simply require() the module again.

Functions scheduled to the queue will run in the worker process context which is completely independent from the main thread. Functions and arguments are serialized to string when passed to the worker process, so you have to make sure that you only pass objects that can be serialized to string and back (functions can). Functions must be self-contained and only reference their own arguments. Arguments can be functions too, same restrinctions apply to them.

Installation

npm install simple-tasks

Usage

Each require('simple-stasks') call created a new worker thread and returns a task queue object for it:

class Queue

    method  push( function, arg1, arg2, ..., callback)

Schedules function to be called with arg1, arg2, ..., resultCallback arguments under worker process context. The callback function will be called when task is finished, it is a required argument (last argument of push() is always interpreted as callback), but you can pass null explicitly instead of it (see first example). The resultCallback argument which is always passed to function can be used to return data back to main thread: simply pass the data as the first argument and it will be delivered as callback first argument.

    method  stop()

Cancels all pending tasks and stops worker process.

Examples

var tasks = require('simple-tasks');

tasks.push(function() {
  console.log("This will be executed by a separate process!");
}, null);

If the function which will run in worker process calls any other user functions, they may be passed to it as arguments. The only limitation is that all arguments must be self-contained.

var tasks = require('simple-tasks');

function printArgument(arg) {
  console.log('argument: ' + arg);
}

function doStuff(printArgument) {
  printArgument('example');
}

tasks.push(doStuff, printArgument, function() {
  console.log('task finished');
});

You can use worker function callback argument for passing data back from the worker process to main. Apart from user provided arguments, the worker process will always pass result callback function to the worker thread. The callback takes one argument which will be passed back to main thread.

var tasks = require('simple-tasks');

function approximatePi(callback) {
  callback(3.1416);
}

tasks.push(approximatePi, function(pi) {
  console.log('The approximate value of Pi is: ' + pi);
});

FAQs

Last updated on 07 Mar 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc