How can I pass a TCPIP handle to a parfeval worker

3 visualizaciones (últimos 30 días)
Minglei Huang
Minglei Huang el 13 de Dic. de 2017
Comentada: Timo Schmid el 14 de Abr. de 2020
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

Respuestas (1)

Edric Ellis
Edric Ellis el 14 de Dic. de 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 comentarios
Minglei Huang
Minglei Huang el 14 de Dic. de 2017
Thanks for answer, I tried this way before. However, the value c returned is empty.
Timo Schmid
Timo Schmid el 14 de Abr. de 2020
I've got the same problem - did you find a solution?

Iniciar sesión para comentar.

Categorías

Más información sobre Asynchronous Parallel Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by