Sending a web request for every 5 minutes in parallel while other MATLAB function is running
2 次查看(过去 30 天)
显示 更早的评论
Hello
I have a scenario where my MATLAB code runs for 2-5 hrs based on the input provided. My issue here is I want to notify my web server which is running over cloud stating that my “MATLAB code is still running” and this should be sent over a web request for every 5 minutes interval.
So here both my MATLAB code and Sending a request should run in parallel. Just in case if any exception occurs then I need to send a failure signal to webserver and then I can exit MATLAB code.
For example consider following functions:
function []= wait_test()
for i=1:20000
d = -1 + (1-(-1)).*rand(1,1);
p(i,1) = 3/d;
if any(d(:) == 0)
warning('Divide by zero')
end
end
I have a function notify_server() which just sends a http POST request with status of code.
So here notify_server() function should be called for every 5 minutes Until for loop of wait_test() function completes its execution or if random number “d” in above code is 0 then we need to call wait_test() and stop execution of both functions.
I didn’t find any matching solution or discussion, If any exists please share it that would help me a lot.
Thanks in Advance
0 个评论
回答(1 个)
Chetan
2023-9-29
I understand that you are attempting to continuously send live connection requests to the server while the computation is ongoing. You can achieve this by utilizing the "timer" function to create a timer that sends the request at specific intervals. Once the computation is complete, you can stop the timer.
To learn how to use the "timer" function and understand the different parameters involved refer the following MathWorks document:
% sample code for demonstration
wait_test()
function [] = wait_test()
% Create a timer object with a specified timer function, period, and execution mode
timerObj = timer('TimerFcn', {@notify_server, "Working"}, 'Period', 1, 'ExecutionMode', 'fixedRate'); % Timer runs every 1 sec
start(timerObj); % Start the timer
try
for i = 1:20
pause(1) % dealy added to show if some computation happens or on
% Perform some calculations or operations
d = -1 + (1-(-1)).*rand(1,1);
p(i,1) = 3/d;
% Check if any of the calculated values are zero
if any(d(:) == 0)
notify_server("", "", 'Failure: Divide by zero'); % Send failure signal to web server
error('Divide by zero'); % Throw an error to stop execution
end
end
catch ex
notify_server(['Failure: ' ex.message]); % Send failure signal with exception message to web server
stop(timerObj); % Stop the timer
delete(timerObj); % Delete the timer object
return; % Exit the MATLAB code
end
notify_server("", "", 'Success'); % Send success signal to web server
stop(timerObj); % Stop the timer
delete(timerObj); % Delete the timer object
end
% the timeer function require 2 additional parameters source and event.
function notify_server(~, ~, status)
% Implement your HTTP POST request code here to notify the web server about the status of the MATLAB code.
% The "status" parameter contains the message to be sent to the web server.
disp(status); % Display the status message in the MATLAB console for demonstration purposes
end
I hope these suggestions help you resolve the issue you are facing.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!