Delay in displaying messages in parallel threads with fileSystemWatcher
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to figure out why there is a delay in the display of the messages in the code below. I have tracked the delay to the spmdReceive() in spmdIndex ==2 and 3, but I am not sure what to do next. The point of the code is to get notified from the FileSystemWatcher when there is a renamed, deleted, or created event and act on that event.
However, I have to create two events in order for the thread to pick up the previous one. Example, if I create "file1.txt", thread 3 is supposed to display that a message like "Worker 3 received: Analysis: file1.txt fom worker 1" immediately. However, I have to create another file (like "new file.txt") to have thread 3 display the message that was supposed to show up previously ("Worker 3 received: Analysis: file1.txt fom worker 1").
The message displayed on the Matlab command line is always one behind, and it applies to both threads (meaning the same thing is happening in thread 2 with the rename/delete action).
function testScript()
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);
ii = 1;
while true
pause(.5); ii = ii+1;
if mod(ii,10) == 0
disp('loop')
end
end
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
disp(datetime('now'))
[data, source] = spmdReceive(1);
disp(['Worker 2 received: ', data, ' from worker ', num2str(source)]);
end
else
while true
disp(datetime('now'))
[data, source] = spmdReceive(1);
disp(['Worker 3 received: ', data, ' from worker ', num2str(source)]);
end
end
end
end
function notifyThread(src,event)
filename = char(event.Name);
eventTypeFromNotify = char(event.ChangeType.ToString);
if strcmp(eventTypeFromNotify, 'Renamed')
disp(datetime('now'))
spmdSend(['Rename: ' filename],2)
end
if strcmp(eventTypeFromNotify, 'Deleted')
disp(datetime('now'))
spmdSend(['Delete: ' filename],2)
end
if strcmp(eventTypeFromNotify, 'Created')
disp(datetime('now'))
spmdSend(['Analysis: ' filename],3)
end
end
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Task Control and Worker Communication dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!