如何访问子进程的进程对象[Node]

How to access the process object of child process [Node]

所以基本上我正在制作一个流程 class 这样我以后就可以轻松地生成自定义流程。我想访问子进程的 process 对象,但是由于某种原因我不能这样做。我的代码:

let cp = require("child_process")
class Process {
    constructor(path, args) {
        this.spawnedProcess = cp.fork(path, args)
    }
    
    getMemoryUsage() {
        return this.spawnedProcess.memoryUsage() //errors here
    }

}

TypeError: this.spawnedProcess.memoryUsage is not a function

如何访问分叉子进程的进程对象?

编辑: 我也试过以下 this.spawnedProcess.process.memoryUsage()(说过程未定义的错误)

正如 how to get a child process memory usage in node.js? 中的回答,这里有一种使用 pidusage 的方法。

let pidusage = require('pidusage');

const cp = require("child_process");

const child = cp.spawn('ls', ['-lh', '/usr']);

pidusage(child.pid, function (err, stats) {

console.log(stats);

});
/*
Output: 
{
   cpu: 10.0,            // percentage (from 0 to 100*vcore)
   memory: 357306368,    // bytes
   ppid: 312,            // PPID
   pid: 727,             // PID
   ctime: 867000,        // ms user + system time
   elapsed: 6650000,     // ms since the start of the process
   timestamp: 864000000  // ms since epoch
}
*/