Skip to content

coderaiser/node-thread-it

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thread It License NPM version Build Status Coverage Status

Drastically simplified worker threads, on node v8 or v10 without --experimental-worker just wraps your code with a promise so you don't have to warry about a thing, just use thread it and it will use workers where can.

Choose any sync module from npm and instead of require use threadIt.

Caution: not all data types can be passed to Worker Thread, for example you just can't pass a function, read carefully what types are supported.

Install

npm i thread-it

API

threadIt(name[, options])

Under the hood threadId uses holdUp so you can use the same options to find a free worker from queue.

  • name - string
  • options - options may contain:
    • log
    • count
    • time

Also you can set THREAD_IT_COUNT env variable to workers count, if 0 it means disabled worker threads.

const threadIt = require('thread-it');

// init workers, depend on os.cpus()
threadIt.init();

const putout = threadIt('putout');
const result = await putout(`const t = 'hello'`);

// when you need to override options use
threadIt('putout', {
    count: 5, // default
    time: 1000, // default
    log: () => {}, // default
});

// terminate workers when no need anymore
threadIt.terminate();

Related

  • hold-up - setInterval with promises, counter and error handling
  • currify - translate the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single or more arguments.
  • fullstore - functional variables.
  • wraptile - translate the evaluation of a function that takes multiple arguments into evaluating a sequence of 2 functions, each with a any count of arguments.

License

MIT

About

Wrap any sync module to thread worker

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published