1. 程式人生 > >系統技術非業餘研究 » erlang:send_after和erlang:start_timer的使用解釋

系統技術非業餘研究 » erlang:send_after和erlang:start_timer的使用解釋

前段時間arksea同學提出這個問題, 因為文件裡面寫的很不明白.

erlang:send_after(Time, Dest, Msg) -> TimerRef
Time = int()
0 <= Time <= 4294967295
Dest = pid() | RegName
LocalPid = pid() (of a process, alive or dead, on the local node)
Msg = term()
TimerRef = ref()
Starts a timer which will send the message Msg to Dest after Time milliseconds.

If Dest is an atom, it is supposed to be the name of a registered process. The process referred to by the name is looked up at the time of delivery. No error is given if the name does not refer to a process.

If Dest is a pid, the timer will be automatically canceled if the process referred to by the pid is not alive, or when the process exits. This feature was introduced in erts version 5.4.11. Note that timers will not be automatically canceled when Dest is an atom.

See also erlang:start_timer/3, erlang:cancel_timer/1, and erlang:read_timer/1.

Failure: badarg if the arguments does not satisfy the requirements specified above.

erlang:start_timer(Time, Dest, Msg) -> TimerRef
Time = int()
0 <= Time <= 4294967295
Dest = LocalPid | RegName
LocalPid = pid() (of a process, alive or dead, on the local node)
RegName = atom()
Msg = term()
TimerRef = ref()
Starts a timer which will send the message {timeout, TimerRef, Msg} to Dest after Time milliseconds.

If Dest is an atom, it is supposed to be the name of a registered process. The process referred to by the name is looked up at the time of delivery. No error is given if the name does not refer to a process.

If Dest is a pid, the timer will be automatically canceled if the process referred to by the pid is not alive, or when the process exits. This feature was introduced in erts version 5.4.11. Note that timers will not be automatically canceled when Dest is an atom.

See also erlang:send_after/3, erlang:cancel_timer/1, and erlang:read_timer/1.

Failure: badarg if the arguments does not satisfy the requirements specified above.

表面上看這2個API沒有什麼大的差別,使用上也一樣, 那為什麼要搞二個呢? 好奇怪!

好, 讓我們來好好研究下典型應用.

這2個API都返回 TimerRef. 使用者可以用這個TimerRef來取消定時器. 唯一的差別是在超時的時候傳送的訊息不同: send_after是Msg, start_timer是{timeout, TimerRef, Msg}.
問題就出在取消timer的時候. 如果這個timer還沒有超時的時候, 那麼取消就沒問題. 如果超時了麻煩就來了, 這個訊息已經有可能已經被放到目標程序的訊息佇列裡,等待派遣處理了.

這時候send_after裡面存放的是Msg, 那使用者如何知道Msg是對於那個TimerRef的呢? 讀者可能說, 那我可以在訊息裡面加入TimerRef. 這個主意不錯, 但是問題是在send_after呼叫返回之前, 你是無法得到TimerRef, 當然也就無從構造這個訊息, 那就無法處理這個可能的超時資訊, 就會破壞邏輯.
所以erts version 5.4.11 引入了, start_timer來解決這個問題. 它是自動的在超時後, 要傳送訊息前, 在訊息裡面添加了{timeout, TimerRef, Msg}, 達到識別的目的.

結論: 文件裡面一眼帶過的東西, 其實是有很多設計方面的考慮, 要認真考慮它的存在的意義.

Post Footer automatically generated by wp-posturl plugin for wordpress.

相關推薦

系統技術業餘研究 » 未公開的erlang ports trace

erlang的trace機制非常強大, 在dbg, ttb模組的配合下, 可以非常清楚的瞭解系統的運作, 排錯, 和調優. 但是有個對於做網路程式重要的功能被忽視了, 沒有寫到文件. 那就是trace ports訊息. 我們的gen_tcp,file都是port實現的, 那麼瞭解這麼模組的運作其實

系統技術業餘研究 » whatsapp深度使用Erlang有感

這麼多年過去了,社群還在討論erlang是不是小眾語言,各種懷疑的時候,whatsapp已經把erlang用到了極致。 whatsapp是什麼? 參見它的 官網 WhatsApp Messenger is a cross-platform mobile messaging app which a

系統技術業餘研究 » 最快的Erlang http hello world 伺服器調優指南 (20Khttp短連結請求/S每桌面CPU)

erl的虛擬機器有2種方式 plain版本的和smp版本的。 smp版本由於鎖的開銷相比要比plain版本的慢很多。而32位機器由於記憶體訪問比64位的少,也會快出很多。所有我選擇在32位的linux系統下調優這個httpd伺服器。這個伺服器就是實現個簡單的功能,在browser下返回hello

系統技術業餘研究 » Literal XML in Erlang with parse_transform/2

One of the things I dislike about Erlang is that it severely impairs bragging opportunities. Yesterday I wrote a module that allows writing literal

系統技術業餘研究 » [Feb 24 2010] Erlang/OTP R13B04 has been released

Erlang/OTP R13B04 has been released. R13B04 is a service release for R13B. There are mostly error corrections, but also some new functionality. This

系統技術業餘研究 » 未公開的erlang:port_s/get_data

我們通常在使用port的時候, 需要把他同其他的上下文關聯起來, 以便在port給我們發生資料的時候, 我們能根據繫結的上下文, 知道如何處理資料. 有2種辦法: 1. 用ets來儲存{Port, Ctx},這個比較慢, 每次都要查表. 2. 用Port本身的空間來儲存Ctx. erlang:po

系統技術業餘研究 » 低成本高效能MySQL雲資料的架構探索

該文已在《程式設計師》2012年10期上發表。 MySQL作為一個低成本、高效能、可靠性好而且開源的資料庫產品,在網際網路企業應用非常廣泛,例如淘寶網有數千臺MySQL伺服器的規模。雖然近兩年來NoSQL的發展很快,新產品層出不窮,但在業務中應用NoSQL對開發者來說要求比較高,而MySQL擁有

系統技術業餘研究 » gen_tcp容易誤用的一點解釋

前天有同學在玩erlang gen_tcp的時候碰到了點小麻煩,描述如下: 比如說連線到baidu.com,發個http請求,然後馬上接收資料,發現接收出錯,wireshark抓包發現數據都有往返傳送,比較鬱悶。 我把問題演示下: $ erl Erlang R14B03 (erts-5.8

系統技術業餘研究 » erlang:send_aftererlang:start_timer的使用解釋

前段時間arksea同學提出這個問題, 因為文件裡面寫的很不明白. erlang:send_after(Time, Dest, Msg) -> TimerRef Time = int() 0 <= Time <= 4294967295 Dest = pid() | RegName

系統技術業餘研究 » Erlang match_spec引擎介紹應用

match_spec是什麼呢? A “match specification” (match_spec) is an Erlang term describing a small “program” that will try to match something (either the para

系統技術業餘研究 » Erlang虛擬機器基礎設施dtrace探測點介紹使用

最新的Erlang虛擬機器(R15B01)很大的一個改進就是加入了對dtrace探測點的支援了, 具體參見這裡, 主要目標是方便在生產實踐中定位複雜的效能問題。 目前Erlang的虛擬機器的探測點支援Linux的systemtap和freebsd的dtrace,我們剛好能夠享受的到。 作者Scot

系統技術業餘研究 » Erlang 網路密集型伺服器的瓶頸解決思路

最近我們的Erlang IO密集型的伺服器程式要做細緻的效能提升,從每秒40萬包處理提升到60萬目標,需要對程序和IO排程器的原理很熟悉,並且對行為進行微調,花了不少時間參閱了相關的文件和程式碼。 其中最有價值的二篇文章是: 1. Characterizing the Scalability of

系統技術業餘研究 » 如何在TILEPro64多核心板卡上編譯執行Erlang

美國Tilera公司的眾核伺服器,單顆核心包含64顆CPU。硬體架構圖: 卡長這樣的: Erlang已經可以在這款CPU上成功執行,我們可以參考Ulf Wiger在Multicore ☺ Message-passing Concurrency 文件中關於Erlang在Tilera上的效能圖

系統技術業餘研究 » Erlangport通訊的資料格式

erlang內建的port非常強大,是erlang通往外面世界的通道,所以port和erlang程式的通訊的資料格式影響了通訊的效率,和穩定性。我們在選擇格式的時候, 會優先考慮到erlang的特性和port程式編寫語言的特點,選出一種2者都容易處理的格式。 通訊通常有2種,基於行的文字和2進位制

系統技術業餘研究 » erlang的profile工具原理優缺點

erlang的tools application下包含了一系列的profile工具, 包括 eprof cprof fprof, 具體的使用可以參看文件和<< erlang effective guide>>. 我這裡要說的是他們的工作原理。 這些模組的核心都是根據erla

系統技術業餘研究 » 轉:CPU密集型計算 erlangC 大比拼

原文地址:http://pseudelia.wordpress.com/2009/08/23/erlang-native-code-benchmark/ Normalerweise compiliert Erlang Bytecode (heißt das so in Erlang?). Das

系統技術業餘研究 » erlang高階原理應用PPT

公司培訓用的 湊合看吧 主要講erlang系統的特點,分佈叢集以及mnesia的使用, 從比較高的角度來看erlang, 讓你有了大體觀. Post Footer automatically generated by wp-posturl plugin for wordpress. No

系統技術業餘研究 » 新的工作研究方向

和大家更新下: 做了將近8年資料庫後,我的工作和研究方向將會延伸到虛擬化和計算相關的雲服務,希望能夠和大家一起進步,Happy New Year! 預祝大家玩得開心! Post Footer automatically generated by wp-posturl plugin for w

系統技術業餘研究 » Erlang 17.5引入+hpds命令列控制程序預設字典大小

Erlang 17.5釋出引入控制程序預設字典大小的命令列引數: Erlang/OTP 17.5 has been released Written by Henrik, 01 Apr 2015 Some highlights of the release are: ERTS: Added co

系統技術業餘研究 » Erlang R16B03釋出,R17已發力

Erlang R16B03釋出了,通常03版本是bug fix版本,進入生產版本,官方的說明如下: OTP R16B03 is a service release with mostly a number of small corrections and user contributions. B