mgr1.0.1 • Public • Published
Easier multithreading for Node.
Because multi-threaded node applications are unwieldy to work with, and I needed to work with them.
Used by new child processes to expose functions (tasks) to parent processes.
It takes two arguments:
- The name of the job (case sensitive).
- A function to call when it's ready.
Jobs will only run when called by their parent process, and must always return a promise. If you've got a sweet Babel setup, I recommend using
async functions. They're way nicer and crafted from unicorn tears.
When a job finishes, the value is sent to the parent process (same goes for errors).
Fork creates a new child node process and aims it at a file path you pass it. Any jobs declared by the child process can be run through
Paths must be absolute.
// Import the Fork class.// The absolute path to a .js file.const file = `/slave-code.js`// Spawns a new process.const slave = file
Starts a job in the child process, returning a promise.
Job names are case-sensitive.
If the job is expecting arguments, you can pass one immediately after the job name.
To keep stuff simple, only one argument is allowed.
$ npm install mgr --save
$ git clone https://github.com/PsychoLlama/mgr.git$ cd mgr$ npm install$ npm run build
First, install from GitHub. npm only has the compiled code.
Be sure to run the build first:
# Run once.$ npm run build# OPTIONAL: watch for changes.$ npm run build -- --watch
Now you can run the tests.
$ npm test
- Abbreviation, "manager"