parallel thread needs to wait for notification from FileSystemWatcher
5 次查看(过去 30 天)
显示 更早的评论
I want ot run at least three threads in parallel. Thread 1 is the FileSystemWatcher, with the rest of the workers doing somethine else very specific, as determined by the action from the FileSystemWatcher.
How would I set this up so that threads 2 and 3 is just sitting and wait to get the spmdSend from thread 1. My code belows throw the error:
Error using testScript
Error detected on worker 2.
Caused by:
Error using testScript
A communication mismatch was encountered during spmdReceive. Worker 1 reached the end of the SPMD block without
completing the communication.ntered during spmdReceive. Worker 1 reached the end of the SPMD block without completing the communication.
spmd
if spmdIndex == 1
try
fswObj = System.IO.FileSystemWatcher('C:\Waves2');
fswObj.Filter = '*.*';
fswObj.NotifyFilter = System.IO.NotifyFilters.FileName;
fswObj.EnableRaisingEvents = true;
addlistener(fswObj,'Renamed',@notifyThread);
addlistener(fswObj,'Deleted',@notifyThread);
addlistener(fswObj,'Created',@notifyThread);
catch ME
fid = fopen('error.txt','w');
fprintf(fid,ME.message);
fprintf(fid,[num2str(ME.stack(1).line), ' : ', ME.stack(1).name, ' : ', ME.stack(1).file]);
fclose(fid);
end
elseif spmdIndex == 2
while true
[data, source] = spmdReceive(1);
disp(['Worker 2 received: ', data, ' from worker ', num2str(source)]);
end
else
while true
[data, source] = spmdReceive(1);
disp(['Worker 3 received: ', data, ' from worker ', num2str(source)]);
end
end
end
cleanup = onCleanup(@()myCleanupFun(fswObj));
function myCleanupFun(fswObj)
fswObj.EnableRaisingEvents = false;
end
function notifyThread(src,event)
filename = char(event.Name);
eventTypeFromNotify = char(event.ChangeType.ToString);
if strcmp(eventTypeFromNotify, 'Renamed')
spmdSend(['Sending to 2 for rename: ' filename],2)
end
if strcmp(eventTypeFromNotify, 'Deleted')
spmdSend(['Sending to 2 for delete: ' filename],2)
end
if strcmp(eventTypeFromNotify, 'Created')
spmdSend(['Sending to 3 for analysis: ' filename],3)
end
end
3 个评论
Handy
2023-8-29
Ahh, I see that you put in the while loop in thread 1 as well to prevent it from completing. I did not think of that.
采纳的回答
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!