Erlang:为列表中的每个元素循环一个函数

时间:2018-04-22 22:35:52

标签: list loops recursion concurrency erlang

我有一个calls.txt:

{john, [jill,joe,bob]}.

{jill, [bob,joe,bob]}.

{sue, [jill,jill,jill,bob,jill]}.

{bob, [john]}.

{joe, [sue]}.

我想遍历Receiver1并为Receiver1中的每个元素调用spawner()函数。但是,当我在控制台中调用read()时,没有任何反应。请帮忙

这是我的代码:

read() -> 
    {ok, [List1, List2, List3, List4, List5]} = 
    file:consult("calls.txt"),
    {Sender1, Receiver1} = List1,
    {Sender2, Receiver2} = List2,
    {Sender3, Receiver3} = List3,
    {Sender4, Receiver4} = List4,
    {Sender5, Receiver5} = List5,
    [spawner(Sender1,H) || H <- Receiver1].

spawner(Sname,Rname) ->
    Sender = spawn(calling,intercept,[]),
    Sender ! {self(), {Sname,Rname}},
    get_feedback().

get_feedback() ->
    receive
        {Msg} -> 
            io:fwrite(Msg)
    end.

1 个答案:

答案 0 :(得分:1)

get_feedback()在主/原始进程中执行。仅仅因为在调用get_feedback()之后调用spawn()并不意味着get_feedback()在生成的进程中执行。在生成的过程中执行的唯一事情是函数calling:intercept()

因此,您最终向生成的进程发送消息,然后在调用receive时在主进程中输入get_feedback() - 这意味着主进程等待消息从产生的过程中,生成的过程不会发送任何消息(或者至少您没有显示生成的进程向主进程发送消息的任何代码)。

如果您在函数get_feedback()内调用calling:intercept(),那么get_feedback()将在生成的进程中执行 - 并且接收将尝试将主进程发送的消息与生成的进程匹配过程

此外,如果由于某种原因,收到的消息不符合您认为应该的消息,那么就像这样重写接收:

receive 
    Msg -> io:format("~w~n", [Msg])
end

该接收将匹配任何消息,然后您可以检查输出以确定您的实际接收不匹配的原因。

或者,您可以让intercept()将msg发送回主进程,然后get_feedback()可以在主进程中收到消息:

-module(my).
-compile(export_all).

read() ->
    {ok, Pairs} = file:consult("calls.txt"),
    [spawner(Sender, Receivers) || {Sender, Receivers} <- Pairs]. 

spawner(Sender, Receivers) ->
    io:format("~w, ~w~n", [Sender, Receivers]),
    Pid = spawn(my, intercept, []),
    io:format("The main process is: ~w.~n", [self()]),
    io:format(
        "The newly spawned process where intercept() is executing is: ~w~n", 
        [Pid]
     ),

    Pid ! {self(), {Sender, Receivers} },
    get_feedback().


intercept() ->
    receive
        {From, Pair} -> 
            io:format("intercept() received message: ~w, from: ~w~n", [Pair, From]),
            From ! {self(), "hello"}
    end.

get_feedback() ->
    receive
        {From, Msg} ->
            io:format("Main process received message: ~p, from: ~w~n", [Msg, From])
    end.

在shell中:

18> c(my).    
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}

19> my:read().
john, [jill,joe,bob]
The main process is: <0.64.0>.
The newly spawned process where intercept() is executing is: <0.168.0>
intercept() received message: {john,[jill,joe,bob]}, from: <0.64.0>
Main process received message: "hello", from: <0.168.0>
jill, [bob,joe,bob]
The main process is: <0.64.0>.
The newly spawned process where intercept() is executing is: <0.169.0>
intercept() received message: {jill,[bob,joe,bob]}, from: <0.64.0>
Main process received message: "hello", from: <0.169.0>
sue, [jill,jill,jill,bob,jill]
The main process is: <0.64.0>.
The newly spawned process where intercept() is executing is: <0.170.0>
intercept() received message: {sue,[jill,jill,jill,bob,jill]}, from: <0.64.0>
Main process received message: "hello", from: <0.170.0>
bob, [john]
The main process is: <0.64.0>.
The newly spawned process where intercept() is executing is: <0.171.0>
intercept() received message: {bob,[john]}, from: <0.64.0>
Main process received message: "hello", from: <0.171.0>
joe, [sue]
The main process is: <0.64.0>.
The newly spawned process where intercept() is executing is: <0.172.0>
intercept() received message: {joe,[sue]}, from: <0.64.0>
Main process received message: "hello", from: <0.172.0>
[ok,ok,ok,ok,ok]