繁体   English   中英

如何访问子进程的进程object【节点】

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

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

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

如何访问分叉子进程的进程 object?

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

正如如何在 node.js 中使用子进程 memory 中回答的那样? ,这是一种使用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
}
*/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM