@shahidcodes/threadifier
Advanced tools
Comparing version 1.0.1 to 1.1.1
module.exports = { | ||
"env": { | ||
"commonjs": true, | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"rules": { | ||
} | ||
}; | ||
env: { | ||
commonjs: true, | ||
es6: true, | ||
node: true, | ||
}, | ||
extends: ['airbnb-base', 'plugin:prettier/recommended'], | ||
globals: { | ||
Atomics: 'readonly', | ||
SharedArrayBuffer: 'readonly', | ||
}, | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
}, | ||
rules: { | ||
'no-plusplus': 'off', | ||
}, | ||
}; |
@@ -11,8 +11,6 @@ const Threadifier = require('../index'); | ||
const args = { name: 'Shahid' } | ||
const args = { name: 'Shahid' }; | ||
Threadifier.run(runTask, args) | ||
.then(console.log) | ||
.catch(console.error) | ||
.catch(console.error); |
@@ -1,1 +0,1 @@ | ||
module.exports = require('./src/lib'); | ||
module.exports = require('./src/lib'); |
{ | ||
"name": "@shahidcodes/threadifier", | ||
"version": "1.0.1", | ||
"version": "1.1.1", | ||
"description": "A wrapper around node worker_thread with easy promise based api.", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"format": "prettier-eslint --write ($pwd).path/'{,!(node_modules)/**/}*.{js,jsx}'" | ||
}, | ||
@@ -22,3 +23,10 @@ "keywords": [ | ||
"devDependencies": { | ||
"eslint": "^6.8.0" | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-config-prettier": "^6.9.0", | ||
"eslint-plugin-import": "^2.19.1", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"prettier": "^1.19.1", | ||
"prettier-eslint-cli": "^5.0.0", | ||
"@types/node": "^13.1.1" | ||
}, | ||
@@ -38,3 +46,5 @@ "engines": { | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"debug": "^4.1.1" | ||
} | ||
} |
@@ -5,4 +5,4 @@ # Threadifier | ||
Node has recently added worker_thread, giving us ability to run long running synchronous tasks in a separate thread. But API is not very seamless. | ||
Threadifier allows you to run any function in different thread seamlessly through its easy to use promise based api. It uses `worker_threads` module to run the given function in a new thread and return the response asynchronously so your event loop keeps running. | ||
Node has recently added worker_thread, giving us ability to run long running synchronous tasks in a separate thread. But API is not very seamless. | ||
Threadifier allows you to run any function in different thread seamlessly through its easy to use promise based api. It uses `worker_threads` module to run the given function in a new thread and return the response asynchronously so your event loop keeps running. Also you can create a worker thread pool to reuse the same threads for various tasks. | ||
@@ -15,6 +15,6 @@ ## Get started | ||
Run a function in different thread. | ||
### Run a function in different thread. | ||
```javascript | ||
const Threadifier = require("@shahidcodes/threadifier"); | ||
const Threadifier = require('@shahidcodes/threadifier'); | ||
@@ -29,3 +29,3 @@ function runTask(arg) { | ||
const args = { name: "Shahid" }; | ||
const args = { name: 'Shahid' }; | ||
@@ -37,2 +37,35 @@ Threadifier.run(runTask, args) | ||
### Create a pool of threads and queue the tasks (recommended) | ||
```javascript | ||
const { WorkerPool } = require('@shahidcodes/threadifier'); | ||
const pool = new WorkerPool(10); | ||
function runTask(args) { | ||
let i = 0; | ||
for (let index = 0; index < 999999999; index++) { | ||
i++; | ||
} | ||
return { i, args }; | ||
} | ||
const args = { name: 'Shahid' }; | ||
for (let index = 0; index < 100; index++) { | ||
pool | ||
.queueTask(runTask, args) | ||
.then(result => console.log(`from worker:`, result)) | ||
.catch(console.error); | ||
} | ||
``` | ||
## General Advice | ||
You should not create many threads at once. If you're going to use this in a request handler/controller then better use a fixed size thread pool. And queue tasks otherwise you'll see CPU spikes when you spawn many many threads than the server can handle. One other reason you should use a thread pool is creating worker thread is expensive so if you're creating threads again n again then better use pool. | ||
## Examples | ||
Please check the [examples](./examples) directory. | ||
## Contribution | ||
@@ -39,0 +72,0 @@ |
@@ -0,1 +1,2 @@ | ||
/* eslint-disable global-require */ | ||
let Worker; | ||
@@ -5,6 +6,9 @@ try { | ||
} catch (err) { | ||
console.error(`worker_threads are not supported in your current node version ${process.version}.`); | ||
console.error( | ||
`worker_threads are not supported in your current node version ${process.version}.`, | ||
); | ||
process.exit(-1); | ||
} | ||
const WorkerPool = require('./pool'); | ||
@@ -21,5 +25,5 @@ function fnWrapper(fn) { | ||
function run(fn, workerData) { | ||
if (!Worker) return; | ||
if (!Worker) return null; | ||
return new Promise((resolve, reject) => { | ||
const wrapped = fnWrapper(fn) | ||
const wrapped = fnWrapper(fn); | ||
const fnStr = wrapped.toString(); | ||
@@ -31,8 +35,8 @@ // console.log(fnStr) | ||
worker.on('exit', code => { | ||
if (code != 0) reject(new Error('worker stopped with non-zero code.')); | ||
}) | ||
}) | ||
if (code !== 0) reject(new Error('worker stopped with non-zero code.')); | ||
}); | ||
worker.on('online', () => console.count('online')); | ||
}); | ||
} | ||
module.exports = { run } | ||
module.exports = { run, WorkerPool }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10030
14
229
74
1
8
2
2
+ Addeddebug@^4.1.1
+ Addeddebug@4.3.7(transitive)
+ Addedms@2.1.3(transitive)