- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有创建其他主管的根主管:
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
RestartStrategy = {one_for_one, 5, 600},
ListenerSup =
{popd_listener_sup,
{popd_listener_sup, start_link, []},
permanent, 2000, supervisor, [popd_listener]},
Children = [ListenerSup],
{ok, {RestartStrategy, Children}}.
popd_listener_sup
主管运行此 gen_server?
最佳答案
根主管
-module(root_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1, shutdown/0]).
start_link() ->
supervisor:start_link({local,?MODULE}, ?MODULE, []).
init(_Args) ->
RestartStrategy = {one_for_one, 10, 60},
ListenerSup = {popd_listener_sup,
{popd_listener_sup, start_link, []},
permanent, infinity, supervisor, [popd_listener_sup]},
Children = [ListenerSup],
{ok, {RestartStrategy, Children}}.
% supervisor can be shutdown by calling exit(SupPid,shutdown)
% or, if it's linked to its parent, by parent calling exit/1.
shutdown() ->
exit(whereis(?MODULE), shutdown).
% or
% exit(normal).
Shutdown
应该设置为
infinity
以给子树足够的时间关闭,并且
Type
应该设置为
supervisor
,这就是我们所做的。
-module(popd_listener_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link({local,?MODULE}, ?MODULE, []).
init(_Args) ->
RestartStrategy = {one_for_one, 10, 60},
Listener = {ch1, {ch1, start_link, []},
permanent, 2000, worker, [ch1]},
Children = [Listener],
{ok, {RestartStrategy, Children}}.
Shutdown
的值设置为
2000
。整数超时值意味着主管将通过调用
exit(Child,shutdown)
告诉子进程终止,然后等待退出信号,原因是从子进程关闭。
-module(ch1).
-behaviour(gen_server).
% Callback functions which should be exported
-export([init/1]).
-export([handle_cast/2, terminate/2]).
% user-defined interface functions
-export([start_link/0]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init(_Args) ->
erlang:process_flag(trap_exit, true),
io:format("ch1 has started (~w)~n", [self()]),
% If the initialization is successful, the function
% should return {ok,State}, {ok,State,Timeout} ..
{ok, []}.
handle_cast(calc, State) ->
io:format("result 2+2=4~n"),
{noreply, State};
handle_cast(calcbad, State) ->
io:format("result 1/0~n"),
1 / 0,
{noreply, State}.
terminate(_Reason, _State) ->
io:format("ch1: terminating.~n"),
ok.
If the gen_server is part of a supervision tree and is ordered by its supervisor to terminate, the function
Module:terminate(Reason, State)
will be called withReason=shutdown
if the following conditions apply:
- the
gen_server
has been set to trap exit signals, and- the shutdown strategy as defined in the supervisor's child specification
is an integer timeout value, not
brutal_kill.
erlang:process_flag(trap_exit, true)
中调用
Module:init(Args)
的原因。
1> root_sup:start_link().
ch1 has started (<0.35.0>)
{ok,<0.33.0>}
ch1
。
ch1
评估正常代码:
2> gen_server:cast(ch1, calc).
result 2+2=4
ok
3> gen_server:cast(ch1, calcbad).
result 1/0
ok
ch1: terminating.
=ERROR REPORT==== 31-Jan-2011::01:38:44 ===
** Generic server ch1 terminating
** Last message in was {'$gen_cast',calcbad}
** When Server state == []
** Reason for termination ==
** {badarith,[{ch1,handle_cast,2},
{gen_server,handle_msg,5},
{proc_lib,init_p_do_apply,3}]}
ch1 has started (<0.39.0>)
4> exit(normal).
ch1: terminating.
** exception exit: normal
ch1
被子主管
popd_listener_sup
重新启动(注意
ch1 has started (<0.39.0>)
)。
supervisor:start_link
中调用
supervisor:start
,而不是
start_link/0
),
exit(normal)
导致 root supervisor 关闭,但它的子进程有一些时间来清理。
关于Erlang 动态主管启动 gen_server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4842871/
当看 abcast abcast man page , 和 cast cast man page ,我无法理解这两者之间有什么区别。 有人可以向我澄清这一点。 谢谢 最佳答案 gen_server:c
有人可以解释gen_server:start()和gen_server:start_link()有什么区别吗? 有人告诉我这是关于多线程的东西。 编辑: 如果我的gen_server是从多个线程中调用
我有一个包含数千个条目的 trie(用元组和列表实现),我想支持并发读取。数据的内存占用在 10-20 MB 范围内。 trie 构建一次,之后只读。 维护状态并为客户端提供并发访问的推荐方法是什么?
我想要做的是让 gen_server 进程接受一个新客户端并立即生成一个新子进程来处理下一个。我看到的问题是,当套接字完成并因此终止时,它也会关闭监听套接字,即使它不再引用它,我也无法弄清楚原因。 知
我有一个主管启动了许多 gen_server。每个 gen_server 都有大量的数据负载,这需要很多时间。我想知道什么时候发生错误,存储在 gen_server 状态及其进程 dict 中的数据是
我尝试将消息转换到 gen_server: gen_server:cast({global, ID}, {watchers}). 处理程序是: handle_cast({watchers}, Sta
我需要编写一个服务器,它将接收来自其他模块的指令并根据收到的指令采取行动。效率是我主要关心的问题。我也用 gen_server还是我自己写服务器。 “我自己的服务器”是指: -module(myser
unregister_name({local,Name}) -> _ = (catch unregister(Name)); unregister_name({global,Name}) ->
免责声明:作者是 OTP 的新手,对 Erlang 的语法、流程和消息有一些基本的了解。 我试图掌握 Erlang 中行为的概念,但我脑海中浮现出许多问题,使我无法理解像 gen_server 这样的
在我的 gen_server 中,我将这样终止它: handle_info({'EXIT', _From, _Reason}, State) -> {stop, partner_fled, S
我想延长 gen_server (创建一个 gen_server_extra )具有一些附加功能。要求是: gen_server_extra进程的行为应该像常规的 gen_server的。例如,他们应
我一直在尝试使用 erlang:monitor/2 来监视 gen_server。不幸的是,每次我尝试这个时,Erlang shell 都会进入无限循环。 这是我为测试这一点而编写的测试程序。 -mo
美好的一天, 我有一个 gen_server 进程,它定期执行一些长时间运行的状态更新任务handle_info: handle_info(trigger, State) -> NewStat
我的 gen_server 包含这样的方法: handle_call(error, From, State) -> io:format("Inside the handle_call erro
我有创建其他主管的根主管: start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) ->
我有 gen_server: start(UserName) -> case gen_server:start({global, UserName}, player, [], []) of
有没有办法告诉 gen_server:“主管已初始化所有 gen_servers,现在您可以发送消息”? 我有一个 worker gen_server,他的工作是在他的监督树中设置其他 gen_ser
我有一个gen_server进程注册了这样的全局名称: global:register_name(>, self()), 另一个进程正在尝试使用gen_server:call向该进程发送消息,如下所示
我正在尝试学习 Erlang 和 OTP,因此我目前正在尝试掌握 gen_server。 我编写了一个快速的 gen_server 实现: -module(test). -behavior(gen_s
Erlang 文档说明了有关 gen_servers 的以下内容: ... Note that for any other reason than normal, shutdown, or {shut
我是一名优秀的程序员,十分优秀!