concurrency - 如何在 Erlang 中对方法应用超时?

标签 concurrency erlang timeout

有没有办法在简单的模块方法调用上应用超时, 例如,

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/

相关文章:

Python ThreadPoolExecutor 线程未完成

erlang - erlang 中的类型与不透明指令

erlang - gen_server :cast to gen_server:abcast 有什么区别

erlang - 功能规范 : -spec. 高效使用

multithreading - 如何在 OS X 内核中实现带有超时的递归互斥锁?

scala - TransactionalMap 与 SynchronizedMap

java - 如何设计一个互斥但独立并发方法的任务队列?

javascript - 当我为它分配一个超时的处理程序时,为什么调整大小事件会不断触发?

java - Spring Boot调用rest ws SocketTimeoutException连接超时

java - ArrayList.clear 抛出 ArrayIndexOutOfBoundsException