See Also: http://www.erlang.org/course/concurrent_programming.html
Definitions
Creating a new process
Simple message passing
An Echo Process
Selective Message Reception
Selection of Any Message
A Telephony Example
Pids can be sent in messages
Registered Processes
The Client Server Model
Timeouts
Definitions定义
Process 轻量级进程,系统会有许多同时运行的process Message 进程间通过消息通讯 Timeout 超时机制 Registered Process 注册了名称的进程 Client/Server Model
Creating a new process创建一个新进程
Pid1进程中执行如下代码 Pid2 = spawn(Mod,Func,Args) Pid2是新进程的标识,只有Pid1知道
Simple Message Passing消息传递
%mesTest.erl -module(mesTest). -export([recvTest/0]). recvTest() -> receive finished -> io:format("program stopped!~n",[]); {From,Msg} -> io:format("Received ~p ~p~n", [From,Msg]) end. 1> c(mesTest). {ok,mesTest} 2> Pid2 = spawn(mesTest,recvTest,[]). <0.39.0> 3> Pid2!{self(),"Hello Pid2"}. Receoved <0.32.0> "Hello Pid2" {<0.32.0>,"Hello Pid2"} 4> Pid2!{self(),"Hello Pid2"}. %Pid2进程已经结束 {<0.32.0>,"Hello Pid2"} 5>
self() 返回自身进程的pid
From 和 Msg 匹配收到的消息 (也可以调用Pid2!finished)
如果From之前已经被bound数据,那只有Msg会收到内容
An Echo Process
% echo.erl -module(echo). -export([go/0, loop/0]). go() -> Pid2 = spawn(echo, loop, []), Pid2 ! {self(), hello}, receive {Pid2, Msg} -> io:format("P1 ~w~n",[Msg]) end, Pid2 ! stop. loop() -> receive {From, Msg} -> From ! {self(), Msg}, loop(); stop -> true end. 执行echo:go().
Selective Message Reception
% echo.erl -module(echo). -export([test/0,selective/0,bfunc/1]). selective() -> receive foo -> io:format("Received foo from A~n",[]) end, receive bar -> io:format("Received bar from B~n",[]) end. bfunc(Cpid) -> Cpid ! bar. test() -> Cpid = spawn(echo, selective, []), spawn(echo, bfunc, [Cpid]), timer:sleep(1000), Cpid ! foo. 1> c(echo). {ok,echo} 2> echo:test(). Received foo from A foo Received bar from B
Selection of Any Message
-module(echo). -export([test/0,selective/0,bfunc/1]). selective() -> receive Msg -> io:format("Received ~w ~n",[Msg]) end, receive Msg2 -> io:format("Received ~w ~n",[Msg2]) end. bfunc(Cpid) -> Cpid ! bar. test() -> Cpid = spawn(echo, selective, []), spawn(echo, bfunc, [Cpid]), timer:sleep(1000), Cpid ! foo. 1> c(echo). {ok,echo} 2> echo:test(). Received bar Received foo foo
A Telephony Example
%这个例子没能理解
Pids can be sent in messages
% A 把自己的Pid发送给B,B再将A的pid给C,最终C直接向A回话 % echo.erl -module(echo). -export([test/0,bfunc/0,cfunc/0]). cfunc() -> receive {_,Apid} -> Apid ! foo end. bfunc() -> receive {Cpid,Apid} -> Cpid ! {"Call A",Apid} end. test() -> %create b and c process Bpid = spawn(echo,bfunc,[]), Cpid = spawn(echo,cfunc,[]), Bpid ! {Cpid,self()}, receive Mes -> io:format("Received ~w from C ~n",[Mes]) end. 1> c(echo). {ok,echo} 2> echo:test(). Received foo from C ok
Registered Processes
register(Alias, Pid) Registers the process Pid with the name Alias.
% echo.erl -module(echo). -export([test/0,bfunc/0,cfunc/0]). cfunc() -> receive {_,Apid} -> Apid ! foo end. bfunc() -> receive {_,Apid} -> cpid ! {"Call A",Apid} end. test() -> %create b and c process Bpid = spawn(echo,bfunc,[]), Cpid = spawn(echo,cfunc,[]), register(cpid,Cpid), %BPid可以通过cpid直接向C发送消息了 Bpid ! {Cpid,self()}, receive Mes -> io:format("Received ~w from C ~n",[Mes]) end.
Client Server Model
% myserver.erl -module(myserver). -export([start/0,server/0,request/1]). handle(Reqdata) -> "Hello " ++ Reqdata. server() -> io:format("server id = ~w ~n",[self()]), receive [From,{request,X}] -> Resp = handle(X), From ! {reply,Resp}, server() end. start() -> register(serverid,spawn(myserver,server,[])). request(Name) -> serverid ! [self(),{request,Name}], receive {reply,Res} -> io:format("Get ~p from server!",[Res]) after 5000 -> io:format("No response from server~n",[]), exit(timeout) end. 1> c(myserver). {ok,myserver} 2> myserver:start(). server id = <0.39.0> true 3> myserver:request("PJ"). server id = <0.39.0> Get "Hello PJ" from server!ok 4>
Uses of Timeouts