Assign different resources to different processes running in background

7 views (last 30 days)
Hi all,
I am trying to set up two independent processes running in the background. The first process would generate data (genData function), while the other would process the data (processData function). The two processes are modular, so I would like to keep them separate. So far, I am sending results from one process to the client, and then to another worker as detailed in this link .
1) is this a good way to have multiple processes running independently, where information might be shared? 2) can I control the resources (# of workers) I can allocate to each process? Should I use instead a backgroundpool? Or does MATLAB somehow allocate the resources the best way possible?, 3) can I get afterEach to run in the background? thank you
% First, create a parallel pool if necessary
if isempty(gcp())
parpool('local', 1);
end
% queue to generate data
q1 = parallel.pool.PollableDataQueue;
% Get the worker to construct a data queue on which it can receive messages from the client
workerQueueConstant = parallel.pool.Constant(@parallel.pool.PollableDataQueue);
% Get the worker to send the queue object back to the client
workerQueueClient = fetchOutputs(parfeval(@(x) x.Value, 1, workerQueueConstant));
% Generate data in one worker
future1 = parfeval(@genData, 1, q1);
% Get the worker to start waiting for messages
future = parfeval(@processData, 1, workerQueueConstant);
%x = [];
for idx = 1:100
%x = [x, randn(1)];
x = poll(q1);
send(workerQueueClient, x);
%send(workerQueueClient, rand(1));
end
% Send [] as a "poison pill" to the worker to get it to stop
send(workerQueueClient, []);
% Get the result
fetchOutputs(future)
% Functions
% Process data
function out = processData(qConstant)
q = qConstant.Value;
out = 0;
while true
data = poll(q, Inf);
if isempty(data)
return
else
out = data;
end
end
end
%Generate data
function x = genData(q1)
x = [];
for jj = 1:10
x = [x, rand(1)];
send(q1,x);
end
end

Answers (0)

Categories

Find more on Parallel for-Loops (parfor) 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!