Is there a way to apply timeout on simple module method call, As example,
my_method(Name)->
timer:sleep(2000),
io:format("hello world ~p!~n",[Name]).
I want to add timeout option to above method, is there a way to do it?
You could spawn your function and wait for a a message back. You can set a timeout while you wait on the receive.
my_method(Name)->
YourTimeOut = 10,
Self = self(),
_Pid = spawn(fun()->
timer:sleep(2000),
io:format("hello world ~p!~n",[Name]),
Self ! {self(), ok} end),
receive
{_PidSpawned, ok} -> ok
after
YourTimeOut -> timout
end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With