Parallelizing tasks in Node.js

后端 未结 5 1434
执念已碎
执念已碎 2020-12-13 02:48

I have some tasks I want to do in JS that are resource intensive. For this question, lets assume they are some heavy calculations, rather then system access. Now I want to r

5条回答
  •  时光说笑
    2020-12-13 03:29

    Depending on your use case you can use something like

    task.js Simplified interface for getting CPU intensive code to run on all cores (node.js, and web)

    A example would be

    function blocking (exampleArgument) {
        // block thread
    }
    
    // turn blocking pure function into a worker task
    const blockingAsync = task.wrap(blocking);
    
    // run task on a autoscaling worker pool
    blockingAsync('exampleArgumentValue').then(result => {
        // do something with result
    });
    

提交回复
热议问题