如何在Matlab中执行系统命令期间知道经过的时间? [英] How to know elapsed time during execution of system command in Matlab?

查看:356
本文介绍了如何在Matlab中执行系统命令期间知道经过的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行系统脚本的Matlab代码.该脚本可能由于命令运行而停止.我想知道程序是否有办法知道是否花费了很长时间并执行其他操作.这是代码:

I have a Matlab code that runs a system script. The script may be stopped due to the command runs. I want to know if there is a way that the program knows if it has taken a long time and do something else. Here is the code:

tic;
[status,cmdout]=system(iperfcmd); % The program can be blocked here
toc; % I want to know if it has taken a long time (like 5 seconds) for the system call and kill it. 

推荐答案

如何异步执行系统调用(

How about performing the system call asynchronously (https://uk.mathworks.com/help/parallel-computing/parallel.pool.parfeval.html) and polling from the main thread, something like this:

% Setup
tMax = 5;         % Maximum time to wait for system call
pollDelay = 1;    % Time between polls
timeOut = false;
% Run system call asynchronously
F = parfeval(@timeSystem,3,'iperfcmd');
tic
% Poll at regular intervals, break out if it finishes or exceeds time limit 
while strcmp(F.State,'running') && ~timeOut
    t = toc;
    pause(pollDelay)
    if t>tMax 
        timeOut = true; % This terminates the loop (alternatively use break)
    end
end

if strcmp(F.State,'finished')
    [status,cmdout,runTime]=fetchOutputs(F);
else
    % Handle hanging system call here
    cancel(F) % Cancelling the FutureEvent might terminate the system call?
end

function [status,cmdout,runTime] = timeSystem(command)
tStart = tic;
[status,cmdout] = system(command)
runTime = toc;
end

希望对您有用.由于没有 iperfcmd

Hopefully that works for you. It's untested tested due to not having iperfcmd

这篇关于如何在Matlab中执行系统命令期间知道经过的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆