All Projects → zy445566 → ncpu

zy445566 / ncpu

Licence: MIT license
multi-threaded library that node.js run function worker

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to ncpu

iworker
Promise-based wrapper for worker_threads
Stars: ✭ 18 (+12.5%)
Mutual labels:  worker, threads
event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (+12.5%)
Mutual labels:  worker, threads
parallelizer
Simplifies the parallelization of function calls.
Stars: ✭ 62 (+287.5%)
Mutual labels:  worker, function
PartialFunctions.jl
A small package to simplify partial function application
Stars: ✭ 34 (+112.5%)
Mutual labels:  function
hex
An ecosystem delivering practices, philosophy and portability. Powered By Deno and JavaScript.
Stars: ✭ 48 (+200%)
Mutual labels:  function
leek
Celery Tasks Monitoring Tool
Stars: ✭ 77 (+381.25%)
Mutual labels:  worker
skeleton-loader
Loader module for webpack to execute your custom procedure. It works as your custom loader.
Stars: ✭ 19 (+18.75%)
Mutual labels:  function
DataTypes
Built-in data types
Stars: ✭ 34 (+112.5%)
Mutual labels:  function
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (+143.75%)
Mutual labels:  worker
serverless-scaleway-functions
Plugin for Serverless Framework to allow users to deploy their serverless applications on Scaleway Functions
Stars: ✭ 58 (+262.5%)
Mutual labels:  function
is-extendable
Answers the question: "can this value have keys?". Returns true if a value is any of the object types: array, regexp, plain object, function or date. Useful for determining if a value is an object that can be extended.
Stars: ✭ 19 (+18.75%)
Mutual labels:  function
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (+12.5%)
Mutual labels:  worker
isolated-runtime
Run untrusted Javascript code in a multi-tenant, isolated environment
Stars: ✭ 21 (+31.25%)
Mutual labels:  threads
async-container
Scalable multi-thread multi-process containers for Ruby.
Stars: ✭ 58 (+262.5%)
Mutual labels:  threads
SACK
System Abstraction Component Kit
Stars: ✭ 18 (+12.5%)
Mutual labels:  threads
terraform-aws-lambda-function
A Terraform module for deploying and managing Lambda functions on Amazon Web Services (AWS). https://aws.amazon.com/lambda/
Stars: ✭ 37 (+131.25%)
Mutual labels:  function
qrcode
QR Code generator function for the FaaS Platform in #golang
Stars: ✭ 17 (+6.25%)
Mutual labels:  function
Example
Metarhia application example for Node.js
Stars: ✭ 147 (+818.75%)
Mutual labels:  threads
Weakify
Provides a way use a method on a class as a closure value that would be referenced by some other component without causing memory leaks.
Stars: ✭ 65 (+306.25%)
Mutual labels:  function
mandelbrot-threaded-webassembly
A simple demonstration of WebAssembly threads
Stars: ✭ 41 (+156.25%)
Mutual labels:  threads

ncpu

multi-threaded library that node.js run function worker

Installation

npm install ncpu

ncpu for the node.js environment,use ncpu-web for the browser environment.

require:Node.js version>=12

Attention

Because it is multithreaded, context information cannot be received and parameter passing can only be achieved by cloning( The cloning will occur as described in the HTML structured clone algorithm, and an error will be thrown if the object cannot be cloned (e.g. because it contains functions)).

Quick Start

import {NCPU} from 'ncpu' // or const {NCPU} = require('ncpu')
async function main () {
    // ### run
    await NCPU.run((a,b)=>a+b,[1,2]) // result: 3
    await NCPU.run((list)=>{
        return list.reduce((total,value)=>{return total+value;});
    },[[1,2,3]]) // result: 6
    // ### pick
    const workerFibo = await NCPU.pick((num)=>{
        const fibo = (value)=>{
            if(value<=2){return 1;}
            return fibo(value-2)+fibo(value-1);
        }
        return fibo(num);
    });
    // slef time to run
    await workerFibo(38)+await workerFibo(39) // result: 102334155 //fibo(40)
    // ### getWorker // reuse a thread
    const ncpuWorker = NCPU.getWorker(); 
    const multiplexingWorkerFibo = await NCPU.pick((num)=>{
        const fibo = (value)=>{
            if(value<=2){return 1;}
            return fibo(value-2)+fibo(value-1);
        }
        return fibo(num);
    }, {ncpuWorker}); // reuse a thread
    const res = await Promise.all([multiplexingWorkerFibo(38), NCPU.run((num)=>{
        const fibo = (value)=>{
            if(value<=2){return 1;}
            return fibo(value-2)+fibo(value-1);
        }
        return fibo(num);
    }, [39] ,{ncpuWorker})]); // reuse a thread
    // ### inject globalData
    await NCPU.run(()=>{
        return require('fs') === require('fs');
    },[],{injectList:['require']}) // result: true
}
main()

The above example spawns a Worker thread for each callback function when runing. In actual practice, use a pool of Workers instead for these kinds of tasks. Otherwise, the overhead of creating Workers would likely exceed their benefit.

Other solutions

License

ncpu is available under the MIT license. See the LICENSE file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].