[英]How to compute execution time in Nodejs ChildProcess.exec
We have an API request that logins our users, like this: 我们有一个API请求可以登录我们的用户,如下所示:
...
//lauches an API request
router.post("/trip/analyse",function(req,res){
t0 = Date.now();
shell = ("sudo /usr/bin/python /mypythonFile");
child = exec(shell, function (error, stdout, stderr) {
if (error) { //There is an error
res.json({"Error" : true});
console.log(Date.now() - t0); // <-------- HERE, How to get this right ?
}else{ //everything went fine
res.json({"Error" : false});
console.log(Date.now() - t0); // <-------- HERE, How to get this right ?
}
});
});
...
I see that the printed execution time is not right, because when many users login at the same time, the "t0" value used for computing the executionTime ("Date.now() - t0") is not correctly computed. 我看到打印的执行时间不正确,因为当许多用户同时登录时,用于计算executionTime的“ t0”值(“ Date.now()-t0”)没有正确计算。 It uses the t0 values of a more recent call of this API request, and not the appropriate t0 value.
它使用此API请求的最新调用的t0值,而不是适当的t0值。
This is because the exec method is asynchronous and take time to execute. 这是因为exec方法是异步的,需要花费一些时间来执行。
How do I right properly the execution time ("Date.now() - t0"), where the original t0 variable is lost ? 我该如何正确执行丢失原始t0变量的执行时间(“ Date.now()-t0”)?
I only had to put 我只需要放
var t0 = Date.now(); var t0 = Date.now();
And it worked ! 而且有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.