https://github.com/coderaiser/node-thread-it
Wrap any sync module to thread worker
https://github.com/coderaiser/node-thread-it
worker-pool workers
Last synced: 6 months ago
JSON representation
Wrap any sync module to thread worker
- Host: GitHub
- URL: https://github.com/coderaiser/node-thread-it
- Owner: coderaiser
- License: mit
- Created: 2019-08-16T09:11:49.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-04-24T14:19:15.000Z (9 months ago)
- Last Synced: 2025-05-26T18:18:20.968Z (8 months ago)
- Topics: worker-pool, workers
- Language: JavaScript
- Homepage:
- Size: 36.1 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Thread It [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]
Drastically simplified [worker threads](https://nodejs.org/dist/latest-v12.x/docs/api/worker_threads.html), 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](https://nodejs.org/dist/latest-v12.x/docs/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist).*
## Install
`npm i thread-it`
## API
### threadIt(name[, options])
Under the hood `threadId` uses [holdUp](https://github.com/coderaiser/hold-up) 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.
```js
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](https://github.com/iocmd/hold-up "Hold Up") - setInterval with promises, counter and error handling
- [currify](https://github.com/coderaiser/currify "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](https://github.com/coderaiser/fullstore "fullstore") - functional variables.
- [wraptile](https://github.com/coderaiser/wraptile "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
[NPMIMGURL]: https://img.shields.io/npm/v/thread-it.svg?style=flat
[BuildStatusIMGURL]: https://travis-ci.com/coderaiser/node-thread-it.svg?branch=master
[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat
[NPMURL]: https://npmjs.org/package/thread-it "npm"
[BuildStatusURL]: https://travis-ci.com/coderaiser/node-thread-it "Build Status"
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"
[CoverageURL]: https://coveralls.io/github/coderaiser/node-thread-it?branch=master
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/node-thread-it/badge.svg?branch=master&service=github