erlang 模块热加载

 

[root@han erlangsrc]# cat codereload.erl 
-module(codereload).
-export([main/0, master_loop/2, worker_loop/0]).
-define(VERSION, "0.2").
 
main() ->
    process_flag(trap_exit, true),
    Pid1 = spawn(?MODULE, worker_loop, []),
    Pid2 = spawn(?MODULE, worker_loop, []),
    spawn(fun() -> register(master, self()), master_loop(Pid1, Pid2) end).
 
 
master_loop(Pid1, Pid2) ->
    io:format("Pid1, Pid2 is alive ~p ~p~n",[is_process_alive(Pid1), is_process_alive(Pid2)]),
    receive
        refresh ->
            io:format("Master code reload~n"),
            Pid1 ! refresh,
            Pid2 ! refresh,
            codereload:master_loop(Pid1, Pid2);
        Any ->
            io:format("Master ~p receive message: ~p~n", [?VERSION, Any]),
            Pid1 ! Any,
            Pid2 ! Any,
            master_loop(Pid1, Pid2)
    end.
 
worker_loop() ->
    receive
        refresh ->
            io:format("Worker code ~p reload~n", [self()]),
            codereload:worker_loop();
        Any ->
            io:format("Worker ~p at ~p also receive message: ~p~n", [?VERSION, self(), Any]),
            worker_loop()
    end.
[root@han erlangsrc]# erl -sname foo@localhost
Erlang/OTP 22 [erts-10.7.2.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [hipe]

Eshell V10.7.2.1  (abort with ^G)
(foo@localhost)1> c(codereload).
{ok,codereload}
(foo@localhost)2> codereload:main().
Pid1, Pid2 is alive true true
<0.94.0>
Master "0.2" receive message: "HI"
Pid1, Pid2 is alive true true
Worker "0.2" at <0.92.0> also receive message: "HI"
Worker "0.2" at <0.93.0> also receive message: "HI"
(foo@localhost)3> 
(foo@localhost)3> c(codereload).
{ok,codereload}
(foo@localhost)4>  {master, 'foo@localhost'} ! refresh.
Master code reload
refresh
Pid1, Pid2 is alive true true
Worker code <0.92.0> reload
Worker code <0.93.0> reload
(foo@localhost)5> 
Master "0.1" receive message: "HI"
Pid1, Pid2 is alive true true
Worker "0.1" at <0.92.0> also receive message: "HI"
Worker "0.1" at <0.93.0> also receive message: "HI"
(foo@localhost)5> 
(foo@localhost)5> {master, 'foo@localhost'} ! "HI".
Master "0.1" receive message: "HI"
"HI"
Pid1, Pid2 is alive true true
Worker "0.1" at <0.92.0> also receive message: "HI"
Worker "0.1" at <0.93.0> also receive message: "HI"
(foo@localhost)6> 
(foo@localhost)6> <0.94.0> ! "HI".
Master "0.1" receive message: "HI"
"HI"
Pid1, Pid2 is alive true true
Worker "0.1" at <0.92.0> also receive message: "HI"
Worker "0.1" at <0.93.0> also receive message: "HI"
(foo@localhost)7> 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Erlang fprof是一个用于性能分析的工具,可以帮助开发者发现并优化Erlang程序中的性能问题。它能够提供详细的函数调用统计信息,帮助开发者找到运行时间最长的函数、占用内存最多的函数等。 使用Erlang fprof进行性能分析非常简单。首先,我们需要在代码中插入一些跟踪代码,以便记录每个函数的运行时间。然后,我们运行程序,并使用fprof:start()函数开启fprof的跟踪功能。接下来,我们可以使用fprof:analyse()函数来生成性能分析报告。报告中包含了各个函数的运行时间、内存使用情况等统计信息。 性能分析报告包含的信息能够指导开发者找到程序的瓶颈所在。通过查看报告中运行时间最长的函数,我们可以确定哪些函数需要进行性能优化。通过查看报告中内存使用最多的函数,我们可以确定哪些函数占用了过多的内存,需要进行内存优化。 除了生成性能分析报告外,Erlang fprof还具备其他有用的功能。例如,我们可以使用fprof:trace/2函数在特定的函数或模块中进行跟踪,以便详细了解函数的调用关系。我们还可以使用fprof:pause()和fprof:resume()函数来暂停和恢复fprof的跟踪。 总而言之,Erlang fprof是一个强大的性能分析工具,能够帮助开发者发现和解决Erlang程序中的性能问题。通过使用fprof,我们可以找到性能瓶颈并进行相应的优化,提升程序的运行效率和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值