How can I send a variable from the client to a running function on a worker?
이전 댓글 표시
If I have a function running on a worker via parfeval or a job, can I have it run in a loop until I send a stop signal to it from the client?
채택된 답변
추가 답변 (1개)
Thomas Falch
2025년 5월 15일
Starting in R2025a, it's possible to use the new "any-destination" PollableDataQueue to greatly simplify Ed's solution. The any-destination queue makes it much simpler to send data from the client to a worker (or from one worker to another).
The new queue is documented here: PollableDataQueue - Send and poll data between client and workers - MATLAB
The code to send a stop signal to a worker could look like this:
queue = parallel.pool.PollalbleDataQueue(Destination="any");
f = parfeval(@runUntilStopSignalReceived, 0, queue);
% Let the function run for a while, then stop it
pause(10);
queue.send("stop")
function runUntilStopSignalReceived(queue)
keepGoing = true
while keepGoing
doSomeWork()
[data, didReceive] = queue.poll()
if didReceive && strcmp(data, "stop")
% We got the stop signal, stop
break;
end
end
카테고리
도움말 센터 및 File Exchange에서 Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!