簡體   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