Simple multithreading library
npm install fibraSimple multithreading library for Node.js using child processes.
npm install --save fibra
`Documentation
`
type Task
= (this: { import: I, api: A }, ...args: any[]) => Promiseclass Fibra
constructor(task: Task, options?: { import?: Imports, api?: A })
run(...args: any[]): Promise
kill(): void
static cores: number
* The amount of CPU cores
type ImportSubItem = string | [string, string]
type ImportItem = string | ImportSubItem[]
interface Imports
[key: string]: ImportItem
* key: module name
* value:
* '' or 'name' for default import
'' or ['*', 'name'] for wildcard import
* ['item', ['anotherItem', 'alias'], ['default', 'Module']] otherwise
`Example
`ts
import Fibra from 'fibra'
import * as fs from 'fs'
import { join as joinPath } from 'path'const api = {
add(a: number, b: number) {
return a + b
}
}
const fibra = new Fibra(
async function (a: number, b: number) {
const { joinPath, fs } = this.import
const { add } = this.api
console.log(typeof joinPath === 'function') // Should be true
console.log(typeof fs === 'object') // Should be true
console.log(await add(a, b)) // Should be 7
const time = Date.now()
while (Date.now() - time < 1000) {}
return 'Hello world' // Shown after 1000 ms
},
{
import: {
path: [['join', 'joinPath']],
fs: '*',
},
api
}
)
setTimeout(() => {
console.log('Non-blocking') // Shown after 500 ms
}, 500)
fibra.run(2, 5).then((res) => console.log(res))
``