How to run .m files in one thread from an app running in a different thread so that I can interact with the app during .m file execution?

7 ビュー (過去 30 日間)
I have an app that I have made programmatically (without appdesigner) that frequently calls already existing functions located in .m files when I click certain buttons. Currently, the app itself and the functions it calls are all run in the foreground. However, these functions can take a long time to execute (several hours), during which time I cannot interact with the app (because callbacks won't execute until after the functions finish running).
The documentation on backgroundPool and parfeval is a bit confusing to me. Is there a way to make the app run on one pool, and the functions in the .m files to run on another thread? I have access to the Parallel Computing Toolboox. Or, would it require running two instances of MATLAB simultaneously? Is it possible to share data across two instances of MATLAB?

回答 (1 件)

Raymond Norris
Raymond Norris 2022 年 9 月 7 日
@Mitchell Tillman you might consider refactoring the code so that the callbacks call parfeval with backgroundPool, but keep in the app running on MATLAB's main execution thread. For instance instead of
function PlotCallback(..)
data = ..
plot(data)
end
call
function PlotCallback(...)
% Spawn a thread to handle the plotting
parfeval(backgroundPool,@plotdata, ..)
% Come back immediately to free up the App
end
I'm leaving an awful lot out here, but hopefully you get the idea
  4 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 9 日
When you have parallel pool, or background thread pool, then the only way the workers can display graphics is to send data to the client for the client to display, or send graphics objects to the client for the client to display.
When you have separate MATLAB instances, then the above methods are still possible (if you use one of several ways to send data between processes.) You also gain the possibility that the second instance is running on the same display and so can display graphics directly in its own area (but not cannot control graphics for the first MATLAB instance.) Running on the same display typically involves the second MATLAB running on the same host, but if you are using Linux with X-Windows then in theory they could be remote; likewise you could in theory be running a remote desktop type of application to display graphics created on a different host.
There are no mechanisms provided to tightly couple multiple MATLAB instances -- at least none other than parfor / spmd / parfeval / batch

サインインしてコメントする。

カテゴリ

Find more on Interactive Control and Callbacks in Help Center and File Exchange

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by