x 秒后终止 childprocess.exec

terminate childprocess.exec after x second

我找不到像子进程等待时间或响应等待时间这样的东西,因为我需要类似的东西。 所以希望有人能在这里帮助我...

如果你看到下面的代码,我正在打印 ros 主题。 然而这个话题还活着,但没有 return 任何东西。 那么如果在 1-2 秒内没有收到任何东西,我怎么能 terminate/kill 这个子进程呢? 因为这正在耗尽我的记忆

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");