有没有办法在简单的模块方法调用上应用超时, 例如,
my_method(Name)->
timer:sleep(2000),
io:format("hello world ~p!~n",[Name]).
我想在上面的方法中添加超时选项,有什么办法吗?
最佳答案
您可以生成您的函数并等待返回消息。您可以在等待接收时设置超时。
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.
关于concurrency - 如何在 Erlang 中对方法应用超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44629823/