gpt4 book ai didi

chromium - puppeteer/ Chrome : handle crashing memory-heavy pages?

转载 作者:行者123 更新时间:2023-12-01 08:23:00 25 4
gpt4 key购买 nike

我有以下代码:

puppeteer.launch().then(async browser => {
for (let id of ids) {
try {
const page = await browser.newPage();
//const url = 'chrome://crash';
await page.goto(url + id)
await page.waitFor(5000);
await page.screenshot({
path: path.join(__dirname, "../public/images/screenshots/" + id + ".png"),
clip: { x: 10, y: 70, width: 780, height: 470}
});
} catch (error) {
console.log('Exception', id, error.message);
page.close();
}
};
browser.close();
});

它通常可以正常工作,但我遇到了特定页面的问题(不幸的是,我无法共享一个 URL)。

此页面尝试加载 GB 数据并导致 Chrome 崩溃,所以我猜它也会导致 Chromium 崩溃。

我从这个页面看到的错误是: Exception 6766 Navigation Timeout Exceeded: 30000ms exceeded .这很好,但它似乎并没有在这一点上停止 - 它导致我的整个服务器挂起,我猜是因为它试图在操作系统级别使用太多内存。

我怎样才能阻止我的服务器挂起并优雅地处理这个问题?我可以向 Chromium 提供标志来限制使用的内存并优雅地放弃吗?我也不确定我的错误处理作为一个整体是正确的,所以任何提示将不胜感激。

最佳答案

一些想法:

首先,这可能与您最初的问题无关,但会节省一些内存。您打开了许多选项卡(页面)而不关闭它们,如果您的 ID 列表很长,这可能会占用大量内存。

尝试通过加载新 url 而不是创建新页面来迭代 ID。

您也可以延长超时以检查是否有更多时间最终可以解决问题。

当您启动 puppeteer 时,您可以传递一些 Chrome 标志。这是完整的Chromium switch list

你可以试试:--unlimited-storage--force-gpu-mem-available-mb .

这也可能有助于调试:--full-memory-crash-report
您可以在那里搜索所有其他与内存相关的标志。

以下是将标志传递给 puppeteer 的方法,以及我提到的其他建议:

puppeteer.launch({args: ['--unlimited-storage', '--full-memory-crash-report']}).then(async browser => {
const page = await browser.newPage();

for (let id of ids) {
try {
//const url = 'chrome://crash';
// Timeout will be 2 min and will wait till network is idle before taking the screenshot
await page.goto(url + id, {timeout: 120000, waitUntil: 'networkidle0'});
await page.screenshot();
} catch (error) {
console.log('Exception', id, error.message);
// If you catch an error you should throw it and handle it on the parent function
// or set up an event listener so you can now what's the state of your app
throw new Err (error);

// Since we are only opening one page and browsing from here
// no need to close when you encounter an error
// just goto the next iteration
// page.close();
}
};
browser.close();
});

关于chromium - puppeteer/ Chrome : handle crashing memory-heavy pages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48297515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com