如何等待用户的输入并继续进行erlang并发编程中的更多代码?

时间:2019-01-31 13:19:02

标签: concurrency erlang erlang-shell

我正在用Erlang进行并发编程,在这里我试图在两个用户之间交换消息。因此,我已经对一组消息进行了硬编码,这些消息作为一个消息从user1逐一发送到另一个user2,我正在从键盘上读取输入,并将该输入作为对user1的答复。但是在执行它时,输入不会被接受,并且消息传递会在没有输入的情况下继续进行,并在所有过程完成后最后要求输入。

-module(third).
-import(lists,[nth/2]).
-export([start/0,user2/5,user1/3,inp/1]).

inp(P) ->
{ok, [S]} = io:fread("entry: \n", "~s"),

P ! S.


user2(0, USERID1,_,_,_) ->
USERID1 ! bye,
io:format("finish");

user2(N, USERID1,Mess,Rep,K) ->
USERID1 ! {message, self(),Mess,K},
receive
    reply ->

    S=self(),
        spawn(third, inp, [S])
end,
user2(N-1, USERID1,Mess,Rep,K+1).

user1(Mess,Rep,K) ->
receive
        bye ->
            io:format("conversation over");
        {message, USERID2,Mess,K} ->
            io:format("~p~n",[nth(K,Mess)]),
            USERID2 ! reply,
            user1(Mess,Rep,K+1)
    end.

start() ->
Mess=["HEY","sup","how are you","yhank you","bye"],
USERID1=spawn(third, user1, [Mess,Rep,1]), 
spawn(third, user2, [5,USERID1,Mess,Rep,1]).

如何等待输入,然后传递该消息。 执行启动模块后,输出如下-

1> c(third).
{ok,third} 
2> third:start().
"HEY"
<0.77.0>
entry: 
"sup"
entry: 
entry: 
"how are you"
entry: 
entry: 
"yhank you"
entry: 
entry: 
"bye"
entry: 
finishentry: 
entry: 
conversation overentry: 

提示输入时,应等待输入,然后继续。

3 个答案:

答案 0 :(得分:1)

稍后给出的版本会按您的预期工作-至少我认为是这样,因为您的问题和代码中都没有很多注释。

它包含2个修改:

  1. 如Odobenus所说,如果生成函数inp,则函数user2 立即获得spawn的返回值,即 生成的过程,而不是用户输入的字符串。因此,user2 别等了该过程仍然存在,因此它将 提示“ entry:”,等待输入,但不会使用它,并且 输入后字符串立即丢失。
  2. 在生成函数user2时,最后一行还有另一个问题。再次,开始 获得新的pid后,函数立即停止 处理;由shell命令启动时,shell将 立即控制io,并且user2进程将永远无法获得 用户输入的字符串,因为它将被shell解释为命令。

'''

-module(third).

-export([start/0,user2/4,user1/2,inp/1]).

inp(P) ->
    S = io:get_line("entry: \n"),
    P ! S.


user2(0, USERID1,_,_) ->
    USERID1 ! bye,
    io:format("finish");

user2(N, USERID1,Mess,K) ->
    USERID1 ! {message, self(),Mess,K},
    io:format("user2 wait for reply~n"),
    receive
        reply ->
            S=self(),
            inp(S)
    end,
    user2(N-1, USERID1,Mess,K+1).

user1(Mess,K) ->
    receive
        bye ->
            io:format("conversation over");
        {message, USERID2,Mess,K} ->
            io:format("~p~n",[lists:nth(K,Mess)]),
            USERID2 ! reply,
            user1(Mess,K+1)
    end.

start() ->
    Mess=["HEY","sup","how are you","yhank you","bye"],
    USERID1=spawn(third, user1, [Mess,1]),
    user2(5,USERID1,Mess,1).

'''

但是,在您的示例中,两个用户进程并没有真正通信:它们不检查消息,并且每次都交换完整的消息集合。我建议您检查该其他版本,默认情况下,在进行5次交流之后,用户1关闭了聊天,但用户2可以通过输入消息“再见”来停止聊天。

'''

-module(third).

-export([start/0,user2/1,user1/1]).


user2(USERID1) ->
    S = io:get_line("entry: \n"),
    USERID1 ! {message, self(),S},
    io:format("user2 wait for reply~n"),
    maybe_continue_user2(USERID1,S).

maybe_continue_user2(_,"bye\n") ->
    io:format("User2 ends the chat~n");
maybe_continue_user2(USERID1,_) ->
    receive
        {reply,"bye"} ->
            io:format("bye, close chat~n");
        {reply,Mess} ->
            io:format("user2 got ~p~n",[Mess]),
            user2(USERID1)
    end.

user1([]) ->
    io:format("User1 ends the chat~n");
user1([H|T]) ->
    receive
        {message,_,"bye\n"} ->
            io:format("user 1: conversation over~n");
        {message, USERID2,Mess} ->
            io:format("~p~n",[Mess]),
            USERID2 ! {reply,H},
            user1(T)
    end.

start() ->
    Mess=["HEY","sup","how are you","thank you","bye"],
    USERID1=spawn(third, user1, [Mess]),
    user2(USERID1).

'''

用户2停止聊天的示例:

9> third:start().
entry:
hi
user2 wait for reply
"hi\n"
user2 got "HEY"
entry:
how are you?
user2 wait for reply
"how are you?\n"
user2 got "sup"
entry:
bye
user2 wait for reply
user 1: conversation over
User2 ends the chat
ok
10>

答案 1 :(得分:0)

看看是否有帮助:

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

start() ->
    register(user1, spawn(my, user1, [["Hello!", "How are you?", "Bye"]]) ),
    register(user2, spawn(my, user2, []) ),
    start_finished.

user1([]) ->
    {user1, terminated};
user1([String|Strings])->
    timer:sleep(500), %% See discussion below.
    user2 ! String,

    receive
        Reply -> io:format("user1 received: ~s~n", [Reply])
    end,

    user1(Strings).

user2() ->
    receive
        "Bye" -> io:format("Bye~n");
        Msg -> 
            Prompt = io_lib:format("~s~n>>> ", [Msg]),
            Reply = io:get_line(Prompt),
            user1 ! Reply,
            user2()
    end.

在外壳中:

~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3  (abort with ^G)

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

2> my:start().
start_finished
Hello!
>>> Hi there!
user1 received: Hi there!

How are you?
>>> Good, and you?
user1 received: Good, and you?

Bye
3> 

我不确定为什么需要进行timer:sleep()呼叫,但是它可以防止导致io:get_line()出现故障的某些竞争状况。

答案 2 :(得分:-1)

从stdin读取的代码。您可以在单独的过程中启动它

spawn(third, inp, [S])

每次user2进程收到“答复”原子

用户输入的结果将被发送到user2进程,并且永远不会使用。