从命令行在Erlang代码中执行并发示例

时间:2015-03-07 00:31:41

标签: erlang erlang-shell

我在Getting Started with Erlang User's Guide并发编程部分测试代码。

tut17.erl中,我使用erl -sname ping启动了一个流程,另一个流程使用al -sname pong作为指导。

-module(tut17).
-export([start_ping/1, start_pong/0, ping/2, pong/0]).

ping(0, Pong_Node) ->
    {pong, Pong_Node} ! finished, 
    io:format("ping finished~n", []);

ping(N, Pong_Node) ->
    {pong, Pong_Node} ! {ping, self()}, 
    receive
        pong ->
            io:format("Ping received pong~n", [])
    end,        
    ping(N - 1, Pong_Node).

pong() ->
    receive
        finished -> io:format("Pong finished~n", []);
        {ping, Ping_PID} -> 
            io:format("Pong received ping~n", []), 
            Ping_PID ! pong,
            pong() 
    end.

start_pong() ->
    register(pong, spawn(tut17, pong, [])).

start_ping(Pong_Node) ->
    spawn(tut17, ping, [3, Pong_Node]).

从ping和pong进程,我可以调用start_ping和start_pong来检查一切正常。

(ping@smcho)1> tut17:start_ping(pong@smcho).
<0.40.0>
Ping received pong
Ping received pong
Ping received pong
ping finished  

(pong@smcho)2> tut17:start_pong().
true
Pong received ping
Pong received ping
Pong received ping
Pong finished 

我正在尝试从命令行运行相同的代码;对于一个简单的hello world示例:

-module(helloworld).
-export([start/0]).

start() ->
    io:fwrite("Hello, world!\n").

我使用以下命令行:

erlc helloworld.erl
erl -noshell -s helloworld start -s init stop

所以,我只是尝试了以下内容,但结果却崩溃了。

  • 来自ping节点:erl -noshell -sname ping -s tut17 start_ping pong@smcho -s init stop
  • 来自pong节点:erl -noshell -sname pong -s tut17 start_pong -s init stop

但是,当ping结束而没有打印任何内容时,我从pong收到此错误报告。

=ERROR REPORT==== 6-Mar-2015::20:29:24 ===
Error in process <0.35.0> on node 'ping@smcho' with exit value: 
    {badarg,[{tut17,ping,2,[{file,"tut17.erl"},{line,9}]}]}

与REPL方法相比,使用命令行,每个进程不会等待对应的响应,但会在一段时间后停止。 什么可能是错的?

2 个答案:

答案 0 :(得分:2)

命令行中的参数在使用-s开关时作为原子列表接收,在使用-run开关时作为字符串列表接收。考虑到这一点,让我们思考一下会发生什么......

此命令从shell发出:

erl -noshell -sname ping \
    -s tut17 start_ping pong@smcho \
    -s init stop

因此使用参数start_ping/1调用[pong@smcho]。然后,它会将ping/2称为ping(3, [pong@smcho]),它在第一行尝试{pong, [pong@smcho]} ! {ping, self()}。因为列表不是消息的有效目标......您的世界会爆炸。

要从Erlang shell和系统shell中轻松地运行它,您可以向start_ping/1添加一个子句:

start_ping([Pong_Node]) ->
    spawn(tut17, ping, [3, Pong_Node]);
start_ping(Pong_Node) ->
    start_ping([Pong_Node]).

答案 1 :(得分:1)

需要进行一些更改。

start_ping

zxq9提示,我修改了start_ping函数。

start_ping([Pong_Node]) ->
    io:format("Ping started~n", []),
    spawn(tut17, ping, [3, Pong_Node]);
start_ping(Pong_Node) ->
    start_ping([Pong_Node]).

当流程结束时调用init:stop()

我不确定这是绝对必要的,但它似乎正在进行此修改。

ping(0, Pong_Node) ->
    {pong, Pong_Node} ! finished, 
    io:format("ping finished~n", []),
    init:stop();

然后我可以从shell命令中删除-s init stoperl -noshell -sname ping -s tut17 start_ping pong@smcho

执行顺序

在调用Ping之前应该调用Pong。

更新后的代码

这是修订后的代码:

-module(tut17).
-export([start_ping/1, start_pong/0, ping/2, pong/0]).

ping(0, Pong_Node) ->
    {pong, Pong_Node} ! finished, 
    io:format("ping finished~n", []),
    init:stop();
ping(N, Pong_Node) ->
    {pong, Pong_Node} ! {ping, self()}, 
    receive
        pong ->
            io:format("Ping received pong~n", [])
    end,        
    ping(N - 1, Pong_Node).

pong() ->
    receive
        finished -> 
            io:format("Pong finished~n", []),
            init:stop();
        {ping, Ping_PID} -> 
            io:format("Pong received ping~n", []), 
            Ping_PID ! pong,
            pong() 
    end.

start_pong() ->
    io:format("Pong started~n", []),
    register(pong, spawn(tut17, pong, [])).

start_ping([Pong_Node]) ->
    io:format("Ping started~n", []),
    spawn(tut17, ping, [3, Pong_Node]);
start_ping(Pong_Node) ->
    start_ping([Pong_Node]).

shell命令:

  • ping:erl -noshell -sname ping -s tut17 start_ping pong@smcho
  • pong:erl -noshell -sname pong -s tut17 start_pong