我获得了parallel computing toolbox的试用版,可以进行一些测试,看看它是如何工作的。
我想要执行的测试之一是看看如何使用此工具箱从GUI中运行一些后台处理并报告处理进度。
到目前为止,我已经创建了一个简单的GUI,其中包含一个按钮,可以在后台启动/取消处理(使用parfeval)和一个标签来报告进展情况。
一切正常(代码在后台运行,我能够处理背景错误或取消),唯一的问题是关于在客户端会话中报告后台处理进度:
function [] = TestBackgroundWorker()
%[
% Create interface
fig = figure();
cuo = onCleanup(@()delete(fig));
stState = uicontrol('Parent', fig, 'Units', 'normalized', 'Position', [0.1 0.7 0.8 0.2], 'Style', 'text', 'String', 'Ready');
btnOkCancel = uicontrol('Parent', fig, 'Units', 'normalized', 'Position', [0.1 0.1 0.8 0.5], 'Style', 'pushbutton', 'String', 'Go', 'Callback', @(s,e)onOkCancelClicked(fig));
% Backstore
data = guidata(fig);
data.bgw = [];
data.stState = stState;
data.btnOkCancel = btnOkCancel;
guidata(fig, data);
waitfor(fig);
%]
end
function [] = onBackgroundProgress(fig, ratio, msg)
%[
% Here I would like to 'BeginInvoke' in client thread
% to refresh 'ratio/msg' in the GUI.
% Below code of course doesn't work:
% 1) It is not synchronized with UI thread
% 2) It is executed in a session with no display
data = guidata(fig);
set(data.stState, 'String', sprintf('%f - %s', ratio, msg));
%]
end
function [] = onOkCancelClicked(fig)
%[
% Backstore
data = guidata(fig);
if (~isfield(data, 'bgw'))
data.bgw = [];
end
if (isempty(data.bgw))
% Start background work
set(data.btnOkCancel, 'String', 'Cancel');
data.bgw = parfeval(@doBackgroundWork, 0, @(r, m)onBackgroundProgress(fig, r, m));
guidata(fig, data);
% Wait for error/termination/cancel
while(true)
try
idx = fetchNext(data.bgw, 0.3);
catch err
if (~isempty(err.cause) && (strcmp(err.cause{1}.identifier, 'parallel:fevalqueue:ExecutionCancelled')))
% Error was due to cancelation
uiwait(msgbox('Processing canceled by user!', 'modal'));
set(data.btnOkCancel, 'String', 'Go', 'Enable', 'on');
else
% Error real error (TODO: display it in some way)
uiwait(msgbox('Processing error!', 'modal'));
set(data.btnOkCancel, 'String', 'Go', 'Enable', 'on');
end
data.bgw = [];
guidata(fig, data);
break;
end
if (isempty(idx))
% Still processing => Enable message pump to read GUI events
drawnow limitrate;
else
% Processing done
uiwait(msgbox('Processing done!', 'modal'));
data.bgw = [];
guidata(fig, data);
set(data.btnOkCancel, 'String', 'Go', 'Enable', 'on');
break;
end
end
else
% Cancel background work
set(data.btnOkCancel, 'String', 'Cancelling...', 'Enable', 'off');
cancel(data.bgw);
end
%]
end
function [] = doBackgroundWork(onProgress)
%[
count = 10;
for k = 1:count,
onProgress((k-1)/count, sprintf('Step %i / %i', k, count));
pause(1);
end
%]
end
我完全理解这个问题,即回调onBackgroundProgress
是从没有显示的会话中执行的,所以没有任何反应(而且它与客户端GUI不同步)。
有没有办法同步并从工作人员向GUI传递数据(在C#中我会使用BeginInvoke
)?可能我没有以适当的方式使用工具箱来实现我想拥有的东西(似乎更倾向于分布式计算而不是多线程),还有另一种方法可以使用这个工具箱吗? ...
修改
我修改了我的代码,将drawnow替换为timer对象(此作品)并尝试使用labSend和labReceive进行同步具有后台会话的UI(这不起作用):
%
% PURPOSE:
%
% Test function to see how to have a responsive GUI while computations
% are running in the background.
%
% SYNTAX:
%
% [] = TestBackgroundWorker();
%
%% --- Main routine
function [] = TestBackgroundWorker()
%[
% Make sure parallel pool is started
gcp();
% Create the interface
% A simple figure with a go/cancel button and a label.
fig = figure();
cuo = onCleanup(@()delete(fig));
stState = uicontrol('Parent', fig, 'Units', 'normalized', 'Position', [0.1 0.7 0.8 0.2], 'Style', 'text', 'String', 'Ready!');
btnStartCancel = uicontrol('Parent', fig, 'Units', 'normalized', 'Position', [0.1 0.1 0.8 0.5], 'Style', 'pushbutton', 'String', 'Start', 'Callback', @(s,e)onOkCancelClicked(fig));
% Backstore for later use
data = guidata(fig);
data.stState = stState;
data.btnStartCancel = btnStartCancel;
guidata(fig, data);
% Wait until figure is closed
waitfor(fig);
%]
end
%% -- Event handler for 'go/cancel' button in the GUI
function [] = onOkCancelClicked(fig)
%[
% Backstore
data = guidata(fig);
if (~isfield(data, 'bgw'))
data.bgw = [];
end
% Depending if background process is running or not
if (isempty(data.bgw))
% Start background work
set(data.btnStartCancel, 'String', 'Cancel');
data.bgw = parfeval(@doBackgroundWork, 0, @(r, m)onBackgroundProgress(fig, r, m));
% Start timer to monitor bgw
data.bgwtimer = timer('ExecutionMode', 'fixedSpacing', 'Period', 0.1, ...
'TimerFcn', @(s,e)onBackgroundCheck(fig, data.bgw));
guidata(fig, data);
start(data.bgwtimer);
else
% Cancel background work
p = gcp('nocreate'); % Make sure parpool is started
if (~isempty(p))
cancel(data.bgw);
end
set(data.btnStartCancel, 'String', 'Cancelling (please wait)...', 'Enable', 'off');
end
%]
end
%% --- Event handlers for monitoring the background worker
function [] = onBackgroundCheck(fig, bgw)
%[
try
idx = fetchNext(bgw, 0.3);
if (isempty(idx)),
% Check for messages from the background worker
if ((numlabs ~= 1) && labProbe)
data = labReceive();
onBackgroundProgress(data{:});
end
else
onBackgroundCompleted(fig);
end
catch err
onBackgroundCompleted(fig, err);
end
%]
end
function [] = onBackgroundCompleted(fig, err)
%[
if (nargin < 2), err = []; end
if (isempty(err))
% Normal completion
uiwait(msgbox('Processing done!', 'Processing', 'help', 'modal'));
elseif (~isempty(err.cause) && (strcmp(err.cause{1}.identifier, 'parallel:fevalqueue:ExecutionCancelled')))
% Error was due to cancelation
uiwait(msgbox('Processing canceled by user!', 'Processing', 'help', 'modal'));
else
% Error real error (TODO: display it in some way)
uiwait(msgbox(sprintf('Processing error: %s', err.message), 'Processing', 'error', 'modal'));
end
data = guidata(fig);
data.bgw = [];
stop(data.bgwtimer);
set(data.stState, 'String', 'Ready!');
set(data.btnStartCancel, 'String', 'Start', 'Enable', 'on');
guidata(fig, data);
%]
end
%% --- Event handler for reporting progression status
function [] = onBackgroundProgress(fig, ratio, msg)
%[
cw = getCurrentWorker();
if (~isempty(cw))
% Were are the background thread so send message to the GUI
% NB: Doing broadcast as I don't know the id of the gui
labBroadcast(labindex, {fig, ratio, msg });
else
% Were are the GUI
data = guidata(fig);
set(data.stState, 'String', sprintf('%f - %s', ratio, msg));
end
%]
end
%% --- Processing to be performed in the background
function [] = doBackgroundWork(onProgress)
%[
count = 15;
for k = 1:count,
onProgress((k-1)/count, sprintf('Step %i / %i', k, count));
pause(1);
end
%]
end
显然labSend
labReceive
<?php get_permalink(); ?>
只能发生在工人之间,而不能发生在客户之间...似乎是一个死胡同。
答案 0 :(得分:2)
如果您的唯一目标是监控员工进度,则可能需要查看parfor progress monitor v2。它还为您提供了如何建立从工作者到客户端的套接字连接的想法,您可以使用它来构建更复杂的UI。