Task.await超时导致GenServer终止

时间:2017-06-13 05:06:25

标签: asynchronous elixir gen-server

我的Genserver在发送一些http请求后会在一段时间后终止。我无法理解原因:

[error] GenServer MyGenServer terminating
** (stop) exited in: Task.await(%Task{owner: #PID<0.420.0>, pid: #PID<0.1054.0>, ref: #Reference<....>}, 5000)
    ** (EXIT) time out
    (elixir) lib/task.ex:416: Task.await/2
    (elixir) lib/enum.ex:966: Enum.flat_map_list/2
    (my_app123) lib/my_genserver.ex:260: MyApp.MyGenServer.do_work/1
    (my_app123) lib/my_genserver.ex:180: MyApp.MyGenServer.handle_info/2
    (stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:683: :gen_server.handle_msg/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: :tick
State: [%{var1: "fdsafdsfd", var2: "43243242"}]

一大堆代码:

  # it's called from handle_info

  def do_work(some_data) do
    Enum.map(some_data, fn(x) ->
      Task.async(fn ->
        case HTTPoison.post(.....) do
        # ...........

是&#34; Task.async&#34;造成超时?但为什么?是的,完成时间可能超过5秒,但为什么会导致异常终止GenServer?如何解决?

关于等待:

If the timeout is exceeded, await will exit; however, the task will continue to run. When the calling process exits, its exit signal will terminate the task if it is not trapping exits.

1 个答案:

答案 0 :(得分:2)

正如文档所说,Task.await的默认超时时间为5秒,然后退出(终止)调用进程。您可以像这样增加超时:

Task.await(task, 60000) # 1 minute

您可以通过将:infinity作为超时而不是数字来完全删除超时:

Task.await(task, :infinity)