简体   繁体   中英

terminate childprocess.exec after x second

i was unable to find anything like child process waiting time, or response waiting time as i need something like that. so hope someone can help me here...

if you see the code below, im printing ros topic. however the topic was alive but didnt return anything. so how can i terminate/kill this child process if it didnt receive anything in 1-2 second. becoz this is eating up my memory right now

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM