[英]Problem with forked child process is killing parent process without showing error
我有一個子進程,它循環通過 colors 來獲取調色板。 它適用於較小的圖像,但是當我上傳大圖像時它會崩潰(4k 崩潰,2k 不會),而不是子進程而是我的整個服務器。 PM2 迅速重新啟動它,但我根本不希望它發生。
這是創建子進程的代碼:
const countColors = fork('count-colors.js', [], {cwd: './modules/node/image-processor', stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ]});
//send the pixel data to the process
countColors.send(pixels);
countColors.on('message', message => {
console.log('message from child:', message);
callback(message.error, message.colors);
return countColors.kill();
});
這段代碼在子進程中(count-colors.js):
process.on('message', data => {
var pixels = data.data;
var colors = [];
for (var i = 0; i < pixels.length; i+=4) {
var hex = hex(pixels[i + 0], pixels[i + 1], pixels[i + 2], pixels[i + 3]);
if (!colors.includes(hex))
colors.push(hex);
if (colors.length>256) return process.send({error: 'Image has over 256 colors'});
}
process.send({colors: colors});
});
不幸的是,它並沒有告訴我除此之外發生了什么:
PM2 | App [www] with id [1] and pid [6160], exited with code [0] via signal [SIGKILL]
PM2 | Starting execution sequence in -fork mode- for app name:www id:1
PM2 | App name:www id:1 online
有沒有辦法防止崩潰,或者至少獲得更多關於它為什么發生的信息? 我應該說上面的代碼在 try {} 塊中,但這似乎並不重要。
我認為 PM2 必須知道進程因SIGKILL
退出的唯一方法是自己發送該信號。
曾經說過,您可以嘗試將消息數據分成塊。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.