javascript - x 秒后终止 childprocess.exec
问题描述
我找不到像子进程等待时间或响应等待时间这样的东西,因为我需要这样的东西。所以希望有人可以在这里帮助我......
如果您看到下面的代码,我正在打印 ros 主题。但是这个话题还活着,但没有返回任何东西。所以如果它在 1-2 秒内没有收到任何东西,我怎么能终止/杀死这个子进程。因为这正在吞噬我的记忆
result = await execute("rostopic echo -n1 --noarr --offset /odom");
function execute(command) {
return new Promise(function(resolve, reject) {
childProcess.exec(command, function(error, standardOutput, standardError) {
if (error) {
reject(error)
}
if (standardError) {
reject(standardError)
}
resolve(standardOutput);
});
}).catch((e) => {logger.crit(FOCNO+"-"+e)});
}
解决方案
const x = 3 /*seconds*/ * 1000;
function execute(command) {
return new Promise(function(resolve, reject) {
command = command.split(' ');
const ps = childProcess.spawn(command[0], command.slice(1));
const killTimeout = setTimeout(() => ps.kill(), x);
ps.stdout.on('data', data => {
clearTimeout(killTimeout);
resolve(data);
});
ps.stderr.on('data', data => reject(data));
}).catch((e) => {logger.crit(FOCNO+"-"+e)});
}
result = await execute("rostopic echo -n1 --noarr --offset /odom");
推荐阅读
- excel - 在模块中使用同名代码
- opencv - ORB-SLAM2 openCV安装错误
- cassandra - Cassandra 中的本机传输请求
- python - TypeError: must be str, not int 无法解决
- node.js - Multer 图像上传中的错误:意外的令牌 - 在位置 0 的 JSON 中,SyntaxError:在位置 0 的 JSON 中的意外令牌#
- mysql - INSERT INTO 正在将空值插入 mySQL 表
- css - 聚合物:根据用户角色(管理员、普通用户、特殊用户)的交替颜色
- javascript - JavaScript 石头剪刀布游戏的游戏计数器不工作
- c++ - 当我将字符串从 .mm 文件传递到 .cpp 文件时,程序编译得很好,但在运行时我在 Xcode 中收到错误“EXC_BAD_ACCESS”
- php - 基于 Woocommerce 中购物车总数的折扣