How can I send data on the fly to a worker when using parfeval?
Show older comments
I'm writing an application which uses tcpip server sockets and I want it to run in the background so that my client is not blocked. I want to send commands to it at runtime not via tcpip. more specifically i tried to send a command to stop listening to incoming requests and then forced it to poll the queue (or whatever it is called) by sending a request over tcp. I don't want it to stop listening when there is an incoming request over tcp because then it can be closed by something other than my client, and I'm generally paranoid. When I tried just canceling the job from the parallel pool it gave me trouble with the socket not being closed properly. I can't seem to send anything to it using labsend and my search brought only dire news that it can't be done. Is there a way to transfer information to a worker started using parfeval at runtime (not when job starts/ends)? Or is there something else that gives me similar functionality and the ability to send information between the workers?
Oh and I don't want to manage my communication using files
Accepted Answer
More Answers (1)
Thomas Falch
on 15 May 2025
Starting in R2025a, it is possible to use the new "any-destination" PollableDataQueue to solve this problem. It makes it much easier to send data from the client to the workers (or from one worker to another) compared with the regular PollableDataQueue. With the new any-destination PollableDataQueue, Edric's example can be rewritten like this:
% First, create a parallel pool if necessary
if isempty(gcp())
parpool('local', 1);
end
% Create a any-destination PollableDataQueue
queue = parallel.pool.PollableDataQueue(Destination="any")
% Get the worker to start waiting for messages
future = parfeval(@doStuff, 1, queue);
% Send a few messages to the worker
for idx = 1:10
send(queue, idx);
end
% Close the queue to make the worker stop
close(queue);
% Get the result
fetchOutputs(future)
% This function gets the worker to keep processing messages from the client
function out = doStuff(queue)
out = 0;
while true
% Wait for a message
[data, didReceiveData] = poll(queue, Inf);
if ~didReceiveData
return
else
out = out + data;
end
end
end
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!