Node.js 子进程 cluster

多进程 HTTP Server

import http from 'http';
import cluster from 'cluster';
import { pid, ppid } from 'process';

function server() {
    let port = 9999;
    let server = http.createServer({}, (request, response) => {
        console.log(`由进程 ${pid} 响应请求`);
        let sab = new SharedArrayBuffer(4);
        let buffer = new Int32Array(sab);
        Atomics.wait(buffer, 0, 0, 5000);
        response.end(`Answer from pid ${pid}`);
    });
    server.listen(port);
}

console.log('进程信息:', { pid, ppid });
if (cluster.isMaster) {
    cluster.fork();
    cluster.fork();
} else {
    server();
}

posted @ 2023-03-09 15:38  develon  阅读(26)  评论(0编辑  收藏  举报