W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
send/2
發(fā)送信息給進(jìn)程,并用receiver/1
接收:iex> send self(), {:hello, "world"}
{:hello, "world"}
iex> receive do
...> {:hello, msg} -> msg
...> {:world, msg} -> "won't match"
...> end
"world"
當(dāng)一個(gè)信息傳送至進(jìn)程,它被存放在進(jìn)程的郵箱中.receive/1
塊會(huì)進(jìn)入當(dāng)前進(jìn)程的郵箱,搜索是否有能模式匹配成功的信息.receive/1
支持衛(wèi)語句和多從句,例如case/2
.
如果郵箱中沒有能夠匹配任何模式的信息,當(dāng)前進(jìn)程會(huì)一直等到能夠匹配的信息出現(xiàn).等待時(shí)間也可以被指定:
iex> receive do
...> {:hello, msg} -> msg
...> after
...> 1_000 -> "nothing after 1s"
...> end
"nothing after 1s"
如果你想要的是已經(jīng)在郵箱中的信息,可以將時(shí)限設(shè)置為0.
讓我們使用這些來在進(jìn)程間通信:
iex> parent = self()
#PID<0.41.0>
iex> spawn fn -> send(parent, {:hello, self()}) end
#PID<0.48.0>
iex> receive do
...> {:hello, pid} -> "Got hello from #{inspect pid}"
...> end
"Got hello from #PID<0.48.0>"
當(dāng)在用戶界面中時(shí),你會(huì)發(fā)現(xiàn)?flush/0
?助手非常有用.它會(huì)刷新并打印郵箱中的所有信息.
iex> send self(), :hello
:hello
iex> flush()
:hello
:ok
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: