Delay in displaying messages in parallel threads with fileSystemWatcher
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Swastik Sarkar
2024 年 11 月 19 日 16:07
I tried to reproduce the issue using the code provided on MATLAB R2023a and observed that the display does lag behind by one operation. However, other operations do not exhibit this lag, the problem is specific to the display of output.
Updating to MATLAB R2024a resolved the issue with disp misbehaving.
Hope this helps
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!