Borrar filtros
Borrar filtros

parallel thread needs to wait for notification from FileSystemWatcher

5 visualizaciones (últimos 30 días)
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 comentarios
Quy
Quy el 29 de Ag. de 2023
Thanks for the comment. I was able to get something to work, but the message displayed is delayed by one event of each core. Anyway around this?
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,50) == 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
[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
end
function notifyThread(src,event)
filename = char(event.Name);
eventTypeFromNotify = char(event.ChangeType.ToString);
if strcmp(eventTypeFromNotify, 'Renamed')
spmdSend(['Rename: ' filename],2)
end
if strcmp(eventTypeFromNotify, 'Deleted')
spmdSend(['Delete: ' filename],2)
end
if strcmp(eventTypeFromNotify, 'Created')
spmdSend(['Analysis: ' filename],3)
end
end
Handy
Handy el 29 de Ag. de 2023
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.

Iniciar sesión para comentar.

Respuesta aceptada

Quy
Quy el 29 de Ag. de 2023
I put in the while loop in the spmdIndex == 1 block of code to prevent the code running to completion. See my comment above.
  1 comentario
Quy
Quy el 29 de Ag. de 2023
But now I have a delay in the threads receiving the data. Please see this question for more info:
https://www.mathworks.com/matlabcentral/answers/2014591-delay-in-displaying-messages-in-parallel-threads-with-filesystemwatcher?s_tid=srchtitle

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by