如何在 MatLab App Designer 中的 parfor 循环下 运行 命名管道流
How to run NamedPipe stream under parforloop in MatLab Appdesigner
我正在 matlab appdesigner 中开发一个软件,它将 运行 在 parforloop 下同时执行两个函数。第一个函数有一个 NamedPipe 对象,错误是: Functionality not supported with figures created with the uifigure function for the app变量,也无法加载 .Net object 。 parforloop下的NamedPipe对象有什么问题?我如何 运行 在 matlab appdesigner 中使用并行的命名管道通信?这是我的代码
function func1(app)
%disp('Function 1');
disp(strcat('Function 1----',datestr(now)));
pause(2);
disp(strcat('Function 1----',datestr(now)));
NET.addAssembly('System.Core');
MypipeServer = System.IO.Pipes.NamedPipeServerStream('my_pipe', ...
System.IO.Pipes.PipeDirection.InOut, ...
System.IO.Pipes.NamedPipeServerStream.MaxAllowedServerInstances,...
System.IO.Pipes.PipeTransmissionMode.Byte,...
System.IO.Pipes.PipeOptions.Asynchronous);
disp("Waiting for client connection...");
MypipeServer.WaitForConnection();
disp("Client Connected");
disp('Reading data from Client');
pause(2);
sr = System.IO.StreamReader(MypipeServer);
%sw = System.IO.StreamWriter('E:/pipeKo.txt')
line = sr.ReadLine();
disp(line);
end
function func2(app)
%disp('Function 2');
disp(strcat('Function 2----',datestr(now)));
pause(2);
disp(strcat('Function 2----',datestr(now)));
end
function ClickOnButtonValueChanged(app, event)
parfor i = 1:2
if i == 1
func1(app);
else
func2(app);
end
end
end
终于,我找到了解决办法。所以我发布了未来帮助的答案。这里应该使用parallel.pool.DataQueue。这是 appdesigner 的完整代码。对于完整的服务器和客户端代码,您还可以查看此 github 存储库:Named Pipe Parallel Communication
methods (Access = private)
function app= func1(app,data)
disp(strcat('Function 1----',datestr(now)));
app.textTextArea.Value=strcat('Function 1----',datestr(now));
pause(5);
disp(strcat('Function 1----',datestr(now)));
NET.addAssembly('System.Core');
MypipeServer = System.IO.Pipes.NamedPipeServerStream('my_pipe', ...
System.IO.Pipes.PipeDirection.InOut, ...
System.IO.Pipes.NamedPipeServerStream.MaxAllowedServerInstances,...
System.IO.Pipes.PipeTransmissionMode.Byte,...
System.IO.Pipes.PipeOptions.Asynchronous);
disp("Waiting for client connection...");
app.textTextArea.Value=[app.textTextArea.Value;"Waiting for client connection..."];
pause(1);
MypipeServer.WaitForConnection();
disp("Client Connected");
app.textTextArea.Value=[app.textTextArea.Value;"Client Connected"];
disp('Reading data from Client');
app.textTextArea.Value=[app.textTextArea.Value;"Reading data from Client"];
pause(1);
sr = System.IO.StreamReader(MypipeServer);
line = sr.ReadLine();
disp(string(line));
app.textTextArea.Value=[app.textTextArea.Value;string(line)];
pause(2);
sw = System.IO.StreamWriter(MypipeServer);
%sw.AutoFlush = true;
time = datestr(now);
disp(time);
app.textTextArea.Value=[app.textTextArea.Value;time];
disp("Server: Hello");
app.textTextArea.Value=[app.textTextArea.Value;"Server: Hello"];
sw.WriteLine("Hello");
sw.Flush();
app.textTextArea.Value=[app.textTextArea.Value;strcat('After Communication----',datestr(now))];
disp(strcat('After Communication----',datestr(now)));
end
function app= func2(app,data)
%disp('Function 2');
app.textTextArea.Value = [app.textTextArea.Value;strcat('Function 2----',datestr(now))];
%pause(1);
app.textTextArea.Value = [app.textTextArea.Value;strcat('Function 2----',datestr(now))];
end
end
methods (Access = private)
% Value changed function: ClickOnButton
function ClickOnButtonValueChanged(app, event)
q = parallel.pool.DataQueue;
r = parallel.pool.DataQueue;
afterEach(q, @app.func1);
afterEach(r, @app.func2);
parfor i = 1:2
if i == 1
%func1(app);
send(q,i);
else
%func2(app);
send(r,i);
end
end
end
end
我正在 matlab appdesigner 中开发一个软件,它将 运行 在 parforloop 下同时执行两个函数。第一个函数有一个 NamedPipe 对象,错误是: Functionality not supported with figures created with the uifigure function for the app变量,也无法加载 .Net object 。 parforloop下的NamedPipe对象有什么问题?我如何 运行 在 matlab appdesigner 中使用并行的命名管道通信?这是我的代码
function func1(app)
%disp('Function 1');
disp(strcat('Function 1----',datestr(now)));
pause(2);
disp(strcat('Function 1----',datestr(now)));
NET.addAssembly('System.Core');
MypipeServer = System.IO.Pipes.NamedPipeServerStream('my_pipe', ...
System.IO.Pipes.PipeDirection.InOut, ...
System.IO.Pipes.NamedPipeServerStream.MaxAllowedServerInstances,...
System.IO.Pipes.PipeTransmissionMode.Byte,...
System.IO.Pipes.PipeOptions.Asynchronous);
disp("Waiting for client connection...");
MypipeServer.WaitForConnection();
disp("Client Connected");
disp('Reading data from Client');
pause(2);
sr = System.IO.StreamReader(MypipeServer);
%sw = System.IO.StreamWriter('E:/pipeKo.txt')
line = sr.ReadLine();
disp(line);
end
function func2(app)
%disp('Function 2');
disp(strcat('Function 2----',datestr(now)));
pause(2);
disp(strcat('Function 2----',datestr(now)));
end
function ClickOnButtonValueChanged(app, event)
parfor i = 1:2
if i == 1
func1(app);
else
func2(app);
end
end
end
终于,我找到了解决办法。所以我发布了未来帮助的答案。这里应该使用parallel.pool.DataQueue。这是 appdesigner 的完整代码。对于完整的服务器和客户端代码,您还可以查看此 github 存储库:Named Pipe Parallel Communication
methods (Access = private)
function app= func1(app,data)
disp(strcat('Function 1----',datestr(now)));
app.textTextArea.Value=strcat('Function 1----',datestr(now));
pause(5);
disp(strcat('Function 1----',datestr(now)));
NET.addAssembly('System.Core');
MypipeServer = System.IO.Pipes.NamedPipeServerStream('my_pipe', ...
System.IO.Pipes.PipeDirection.InOut, ...
System.IO.Pipes.NamedPipeServerStream.MaxAllowedServerInstances,...
System.IO.Pipes.PipeTransmissionMode.Byte,...
System.IO.Pipes.PipeOptions.Asynchronous);
disp("Waiting for client connection...");
app.textTextArea.Value=[app.textTextArea.Value;"Waiting for client connection..."];
pause(1);
MypipeServer.WaitForConnection();
disp("Client Connected");
app.textTextArea.Value=[app.textTextArea.Value;"Client Connected"];
disp('Reading data from Client');
app.textTextArea.Value=[app.textTextArea.Value;"Reading data from Client"];
pause(1);
sr = System.IO.StreamReader(MypipeServer);
line = sr.ReadLine();
disp(string(line));
app.textTextArea.Value=[app.textTextArea.Value;string(line)];
pause(2);
sw = System.IO.StreamWriter(MypipeServer);
%sw.AutoFlush = true;
time = datestr(now);
disp(time);
app.textTextArea.Value=[app.textTextArea.Value;time];
disp("Server: Hello");
app.textTextArea.Value=[app.textTextArea.Value;"Server: Hello"];
sw.WriteLine("Hello");
sw.Flush();
app.textTextArea.Value=[app.textTextArea.Value;strcat('After Communication----',datestr(now))];
disp(strcat('After Communication----',datestr(now)));
end
function app= func2(app,data)
%disp('Function 2');
app.textTextArea.Value = [app.textTextArea.Value;strcat('Function 2----',datestr(now))];
%pause(1);
app.textTextArea.Value = [app.textTextArea.Value;strcat('Function 2----',datestr(now))];
end
end
methods (Access = private)
% Value changed function: ClickOnButton
function ClickOnButtonValueChanged(app, event)
q = parallel.pool.DataQueue;
r = parallel.pool.DataQueue;
afterEach(q, @app.func1);
afterEach(r, @app.func2);
parfor i = 1:2
if i == 1
%func1(app);
send(q,i);
else
%func2(app);
send(r,i);
end
end
end
end