zoukankan      html  css  js  c++  java
  • [Node.js] Child Process with fork() to handle heavy calculation process

    When build server, if we have a API endpoint requires some heavy calculation process, it will block the whole world. In this post, we will see how to use 'child_process' fork() to solve the problem;

    Let's see the blocking code example:

    const http = require('http');
    
    const longComputation = () => {
        let sum = 0;
        for (let i = 0; i < 1e9; i++) {
            sum += i;
        }
        return sum;
    }
    
    const server = http.createServer();
    
    server.on('request', (req, res) => {
        if (req.url === '/compute') {
            const sum = longComputation();
            return res.end(`Sum is ${sum}`);
        } else {
            res.end('Ok');
        }
    })

    When we request '/compute' API endpoint, because of 'longCompute', it blocks the world, no other request can go thought.

    curl localhost:3000/compute

    Let's see how to fix it:

    1. We will create a compute.js to hold the 'longCompute' function and also listen to the 'message' event on the global:

    // Compute.js
    const longComputation = () => {
        let sum = 0;
        for (let i = 0; i < 1e9; i++) {
            sum += i;
        }
        return sum;
    }
    
    // listen the mssage event on the global
    // then do the computation
    process.on('message', (msg) => {
        const sum = longComputation();
        process.send(sum);
    })

    2. In server.js, instead of calling 'longCompute' directly, we fork the compute.js;

    Send signal to tell the compute file to start doing the job, then we also need to listen to the event come back after computation is done.

    // start processing
    compute.send('start');
    // listen to the message
    compute.on('message', sum => {
        res.end(`Sum is ${sum}`);
    });

    Full code for server.js

    const http = require('http');
    const {fork} = require('child_process');
    
    const server = http.createServer();
    
    server.on('request', (req, res) => {
        if (req.url === '/compute') {
            const compute = fork('compute.js');
            // start processing
            compute.send('start');
            // listen to the message
            compute.on('message', sum => {
                res.end(`Sum is ${sum}`);
            });
        } else {
            res.end('Ok');
        }
    });
    server.listen(3000)
  • 相关阅读:
    php RSA加密传输代码示例
    数据库执行语句时,严重注意类型转换的问题
    数据库sql的in操作,解决in的过多
    php利用自定义key,对数据加解密的方法
    修改目录下所有文件的某段内容
    ajax返回json时,js获取类型,是字符串类型
    浅析单点登录,以及不同二级域名下的SSO实现
    samba服务,连接远程开发机
    『转』统计一个日志文件里,单词出现频率的shell脚本
    python的装饰器
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10498591.html
Copyright © 2011-2022 走看看