如果gen_server在监控树中不需要stop函数,gen_server会由其supervisor根据shutdown策略自动终止掉.如果要在进程终止之前执行清理,shutdown策略必须设定一个timeout值而不是brutal_kill并且gen_server要在init设置trap_exit.当被supervisor命令shutdown的时候,gen_server会调用terminnate(shutdown,State),特别注意: 被supervisor终止掉,终止的原因是Reason=shutdown,这个我们之前也

init(Args) ->
...,
process_flag(trap_exit, true),
...,
{ok, State}.
...
terminate(shutdown, State) ->
..code forcleaning up here..
ok.
如果gen_server不是supervisor的一部分,stop方法就很有用了:
...
export([stop/0]).
...
stop() ->
gen_server:cast(ch3, stop).
...
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast({free, Ch}, State) ->
....
...
terminate(normal, State) ->
ok.
通过调用terminate方法,gen_server可以优雅的关闭掉了. 如果结束的消息不是normal,shutdowngen_server就会被认为是异常终止并通过error_logger:format/2产生错误报告.
Note: if any reason other than normal, shutdown or {shutdown, Term} is used whenterminate/2 is called, the OTP framework will see this as a failure and start logging a bunch of stuff here and there for you.
 

如何设置gen_server在退出时执行相关操作的更多相关文章

  1. 在RAC执行相关操作发生ora-01031:insufficient privileges解决方法

    grid用户下 寻找命令的路径 如which crsctl su - root用户 然后根据找到的路径去执行命令 如 /u01/app/oracle/product/11.2.0/db_1/bin/c ...

  2. register_shutdown_function函数详解--脚本退出时执行回调函数

    register_shutdown_function — Register a function for execution on shutdown. ps:Registers a callback  ...

  3. 判断iframe加载完成、用于当ifame加载完成时执行一些操作

    window.frames["iframec"].addEventListener( "load", function(){ window.frames[&qu ...

  4. Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作

    前言:通常,我们写的公共的模块给别人用,但是这个模块又必须在特定的线程中执行. 比如,一个加载网络图片的的方法,需要在子线程中执行. /** * 加载网络图片 */ private void load ...

  5. linux通过expect工具来实现自动登录服务器,并执行相关操作

    参考地址:https://www.cnblogs.com/liyuanhong/articles/7728034.html EOF的使用参考:https://www.cnblogs.com/liyua ...

  6. git设置github的远程仓库的相关操作

        git能够把github作为远程仓库,本地可以进行推送有关变更,从而多人可以进行协作开发工作.    1  ssh-keygen -t rsa -C "your-email@163. ...

  7. SpringBoot项目启动时执行初始化操作

    SpringBooot中的CommandLineRunner接口会在所有Spring Beans初始化之后,SpringApplication.run()之前执行. 1.添加pom引用 <?xm ...

  8. JQuery设置checkbox选中或取消等相关操作

    $("[name='checkbox']").attr("checked",'true');//全选 $("[name='checkbox']&quo ...

  9. angular 监听离开页面执行相关操作

    $scope.$on("$destroy", function() { //...})

随机推荐

  1. Lucence.Net+添加关键词+分页+排序

    1.使用queryparser完成解析搜索请求 2.基本格式如: QueryParser parser=new QueryParser("字段名称","分析器实例&quo ...

  2. iptables 简单配置

    通过命令 netstat -tnl 可以查看当前服务器打开了哪些端口  Ssh代码   netstat -tnl     查看防火墙设置  Ssh代码   iptables -L -n      开放 ...

  3. C#代码模拟http发送get和post请求

    private string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)W ...

  4. 简单的UIScrollView 下拉刷新

    这里只贴主要代码 #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate&g ...

  5. ThinkPHP第十三天(CONF_PATH、APP_PATH,UEditor用法)

    1.CONF_PATH 项目配置文件目录地址,APP_PATH 项目地址 2.ThinkPHP中更新数据的连接操作位save(),更新一个字段可以用setField(name,value)方法. 3. ...

  6. IOS 学习笔记(1) 视图UIViewController

    1.UIViewController *newController=[[UIViewController alloc] initWithNibName:@"XXX" bundle: ...

  7. GC算法之串行并行并发

    串行收集器: 用单线程处理所有垃圾回收工作,因为无需多线程交互,所以效率比较高.但是,也无法使用多处理器的优势,所以此收集器适合单处理器机器.当然,此收集器也可以用在小数据量(100M左右)情况下的多 ...

  8. Delphi程序自删除的几种方法

    program Project1; uses SysUtils, windows; var f:textfile; a:string; begin a:=paramstr(); assignfile( ...

  9. openScales源码学习系列之 Feature属性

    coordinates:当前区域,geometry或Polygon的点集合. countries.国家所在位置下标 SQKM.平方千米 COLOR_MAP.该国家的颜色类别 SQMI.平方英里 CON ...

  10. HDU 1507 Uncle Tom's Inherited Land*

    题目大意:给你一个矩形,然后输入矩形里面池塘的坐标(不能放东西的地方),问可以放的地方中,最多可以放多少块1*2的长方形方块,并输出那些方块的位置. 题解:我们将所有未被覆盖的分为两种,即分为黑白格( ...