【发布时间】:2017-01-26 11:25:19
【问题描述】:
我有这个代码,它是一个基本的 OTP 服务器:
defmodule Clients do
use Application
require Logger
def stuff do
Logger.debug "Clients.start/0"
# {:ok, pid} = Task.Supervisor.start_child(Clients.TaskSupervisor, fn -> doclient end)
end
def doclient(port) do
Logger.debug "Clients.doclient/0"
{:ok, socket} = :gen_tcp.listen(port,[:binary, packet: :line, active: false, reuseaddr: true])
#if this is under 2000ms there the process exits with a shutdown error
:timer.sleep(1500)
end
def start(_type, _args) do
import Supervisor.Spec
Logger.debug "Clients.init/1"
children = [
supervisor(Task.Supervisor, [[name: Clients.TaskSupervisor]]),
worker(Task, [Clients, :doclient, [4040]])
]
opts = [strategy: :one_for_one, name: Clients.Supervisor]
Supervisor.start_link(children, opts)
end
end
iex 的输出是:
iex(1)> Clients.start(1,2)
20:07:19.879 [debug] Clients.init/1
20:07:19.889 [debug] Clients.doclient/0
{:ok, #PID<0.141.0>}
iex(2)>
20:07:21.402 [debug] Clients.doclient/0
20:07:22.909 [debug] Clients.doclient/0
20:07:24.413 [debug] Clients.doclient/0
** (EXIT from #PID<0.139.0>) shutdown
如果 doclient/1 计时器调用不到 2 秒,则会发生上述关闭,否则它会愉快地滴答作响。我不确定为什么?如果我想让 doclient/1 执行一些花费不到 2 秒的任意代码(这不是我想象的可靠时间段),这个调用总是会爆炸。
【问题讨论】:
标签: elixir erlang-otp erlang-supervisor