javascript - API调用期间的NodeJs MaxListenersExceededWarning
问题描述
我正在用风数据(从 API JSON 中抓取)填充一个数组,但出现MaxListenersExceededWarning
错误。我查了一下,似乎是由于代码中的错误。一种解决方法是设置setMaxListeners(n);
,但显然不建议这样做。
谁能看到是什么导致这么多听众注册?有什么解决办法?我正在创建一个windRecordings
在请求时吐出数组的 API。
代码
const getWindForecast = (windRecordings) => {
setInterval(() => {
const instantWind = scrapeAPI(
"http://mobvaer.kystverket.no/v2/api/stations/5265049"
);
instantWind.then((res) => {
if (windRecordings.length > 0) {
// A wind value(s) is already pushed to the list
const latestRecordedWind = windRecordings[windRecordings.length - 1]; // get the first element out
// Compare the lates wind value in the list to the lates API request wind value
if (
latestRecordedWind[1]["Value"]["Value"] == res[1]["Value"]["Value"]
) {
console.log("They are the same");
} else {
console.log("They are not the same, push them.")
windRecordings.push(res);
}
} else {
// The length is less than 0, no element has been added so far, push element
console.log("Push the first to the list");
windRecordings.push(res);
}
});
return windRecordings;
}, 1000);
};
错误
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:85830) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:85830) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGTERM listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:85830) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGHUP listeners added to [process]. Use emitter.setMaxListeners() to increase limit
scrapeAPI 函数
const puppeteer = require("puppeteer");
const scrapeAPI = async (url) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
var content = await page.content();
innerText = await page.evaluate(() => {
return JSON.parse(document.querySelector("body").innerText);
});
const instantWind = innerText["Instantaneous"];
return instantWind;
};
干杯!
解决方案
您每秒钟都在启动一个新的浏览器实例,并且不要关闭它们。
您的代码执行此操作:
setInterval(() => {
//const instantWind = scrapeAPI();
...
const browser = await puppeteer.launch();
...
}, 1000);
您需要关闭重用浏览器实例或至少关闭它们:
const scrapeAPI = async (url) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
var content = await page.content();
innerText = await page.evaluate(() => {
return JSON.parse(document.querySelector("body").innerText);
});
const instantWind = innerText["Instantaneous"];
await browser.close();
return instantWind;
};
推荐阅读
- c++ - 有没有办法在 C++ 中实现非成员 [] 运算符?
- apache-spark - 如何在桶列上使用分区动态覆盖?
- html - 空间不足时如何创建滚动功能
- python - 格式化大型列表理解的最佳实践?
- python - Selenium Python - 无法通过 Xpath 选择
- c# - 如何使用 CefSharp 在 winforms 中授权代理(登录名:密码:ip:端口:连接类型)?
- c# - NET5 JWT 不记名身份验证不识别 SSL 证书
- github-pages - 从 haproxy 转发到 github 页面:获取重定向而不是反向代理直通
- asp.net-mvc - 使页面响应并失去对输入字段的关注
- powershell - 从服务器中提取打印驱动程序并使用 Powershell 将它们安装到工作站