gpt4 book ai didi

matlab - 如何知道在 Matlab 中执行系统命令期间耗时?

转载 作者:行者123 更新时间:2023-12-04 10:37:21 25 4
gpt4 key购买 nike

我有一个运行系统脚本的 Matlab 代码。由于命令运行,脚本可能会停止。我想知道是否有办法让程序知道它是否花了很长时间并做其他事情。这是代码:

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.

最佳答案

如何异步执行系统调用( https://uk.mathworks.com/help/parallel-computing/parallel.pool.parfeval.html )并从主线程轮询,如下所示:

% 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

关于matlab - 如何知道在 Matlab 中执行系统命令期间耗时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60101154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com