How can I pass a TCPIP handle to a parfeval worker

3 views (last 30 days)
In my special case, I need to construct a TCPIP handle before running the parfeval. So I was thinking to pass the TCPIP handle to parfeval. However, it looks like the worker runs under a different workspace than the workspace where TCPIP handle I was constructed. Any idea how to solve this problem ? Thanks
main code
port = tcpip('192.xxx.xx.xx', xxx);
fopen(port);
Request_message = ['aaaaaa'];
fwrite(port, Request_message );
value = [];
parpool(2)
for i = 1:20
f1 = parfeval(@f1_worker,1,port);
if ~isempty(value)
f2 = parfeval(@f2_worker,1,value);
end
[completedIdx,value] = fetchNext([f1]);
end
fclose(port);
delete(port);
delete(gcp('nocreate'));
worker
function [message] = f1_worker(port)
message = fread(port);
end

Answers (1)

Edric Ellis
Edric Ellis on 14 Dec 2017
The workers executing the body of your parfeval requests are separate processes, so you cannot send objects of class tcpip to the workers. You should instead use parallel.pool.Constant to build the tcpip objects directly on the workers, and use them from there. A bit like this (completely untested):
% We must open the pool up front
parpool(2);
% Build the tcpip on each worker
c = parallel.pool.Constant(@iBuildTcpip);
% Here's the builder function
function port = iBuildTcpip()
port = tcpip('192.xxx.xx.xx', xxx);
fopen(port);
Request_message = ['aaaaaa'];
fwrite(port, Request_message );
end
% Now submit parfeval requests using "c"
parfeval(@iDoStuff, 1, c);
% where iDoStuff is something like
function out = iDoStuff(c)
port = c.Value; % Unpack the parallel.pool.Constant
out = fread(port);
end
  2 Comments
Minglei Huang
Minglei Huang on 14 Dec 2017
Thanks for answer, I tried this way before. However, the value c returned is empty.
Timo Schmid
Timo Schmid on 14 Apr 2020
I've got the same problem - did you find a solution?

Sign in to comment.

Categories

Find more on Asynchronous Parallel Programming 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!