《The Event Loop and Dart》譯文
原文:https://webdev.dartlang.org/articles/performance/event-loop
Event loops and queues
event loop循環(huán)的從event queue中取出一個event并執(zhí)行。
event queue中的event可以是用戶輸入、文件IO、定時器等等:
Dart’s single thread of execution
一旦一個dart function開始執(zhí)行,就會繼續(xù)執(zhí)行下去直到它運行結(jié)束,換句話說,一條dart語句不能被其他的dart語句打斷。
注意: A Dart command-line app 可以在不同的isolate上平行的執(zhí)行代碼。(Dart web app 不能直接創(chuàng)建額外的isolate,但可以創(chuàng)建worker) isolate相互隔離、不共享內(nèi)存、通過傳遞消息來完成之間的通信。 除顯示調(diào)用方法將dart語句放在isolate或者worker以外,dart語句全部運行在主線程。更多的信息可以參考 Use isolates or workers if necessary 。
Dart APP當在主isolate中調(diào)用完main()方法后,就按順序循環(huán)執(zhí)行event queue中的event,如下圖:
Dart’s event loop and queues
Dart APP存在一個單獨的event loop和兩個event queue(event queue 和 microtask queue)。
Microtask Queue存在的意義是在處理一個event時,部分語句稍后執(zhí)行,但希望在下一個event之前處理完畢。
例如,當一個狀態(tài)發(fā)生改變時,它將若干變化組合一起同步報告。microtask queue使得在DOM在顯示狀態(tài)變化前,狀態(tài)可發(fā)生多次變化。
event queue包含了dart自定義event、系統(tǒng)event,但microtask queue只包含了自定義event,但我們期望Web實現(xiàn)包含瀏覽器microtask queue。
注意:當event looper正在處理microtask queue中的Event時候,event queue中的event就停止了處理了,此時App不能繪制任何圖形,不能處理任何鼠標點擊,不能處理文件IO等等
Event-Looper優(yōu)先執(zhí)行Microtask Queue中的Event,直到Microtask Queue為空時,才會執(zhí)行Event Queue中的Event
Dart中只能知道Event處理的先后順序,但是并不知道某個Event執(zhí)行的具體時間點,因為它的處理模型是一個單線程循環(huán),而不是基于時鐘調(diào)度(即它的執(zhí)行只是按照Event處理完,就開始循環(huán)下一個Event,而與Java中的Thread調(diào)度不一樣,沒有時間調(diào)度的概念),也就是我們既是指定另一個Delay Time的Task,希望它在預期的時間后開始執(zhí)行,它有可能不會在那個時間執(zhí)行,需要看是否前面的Event是否已經(jīng)Dequeue。
鏈式調(diào)用明確語句順序
如果語句之間存在依賴關(guān)系,那就明確依賴關(guān)系。明確的依賴關(guān)系讓別人能更容易理解。
下面是一種錯誤的用法:
// BAD because of no explicit dependency between setting and using
// the variable.
future.then(...set an important variable...);
Timer.run(() {...use the important variable...});
應(yīng)該替換為:
// BETTER because the dependency is explicit.
future.then(...set an important variable...)
.then((_) {...use the important variable...});
好的做法是用then()去明確變量設(shè)置和使用的前后順序。(如果不管依賴是否正確執(zhí)行,就要執(zhí)行后續(xù)的語句,可以使用whenComplete()去代替when())
如果變量設(shè)置是異步操作,建議將變量使用語句放在new Future中。
// MAYBE EVEN BETTER: Explicit dependency plus delayed execution.
future.then(...set an important variable...)
.then((_) {new Future(() {...use the important variable...})});
How to schedule a task
當有代碼需要在后續(xù)任務(wù)執(zhí)行的時候,可以通過dart:async提供的兩種方法實現(xiàn):
1.使用Future類,可以將任務(wù)加入到Event Queue的隊尾
2.使用scheduleMicrotask函數(shù),將任務(wù)加入到Microtask Queue隊尾
Use the appropriate queue (usually: the event queue)
盡可能使用event queue,microtask queue會阻塞event queue的執(zhí)行。
如果語句需要在event queue中其他event之前執(zhí)行,可以通過scheduleMicrotask()添加到microtask queue。
Event queue: new Future()
可以通過new Future() 或者 new Future.delayed()來使用event queue。
注意:也可以使用Timer來調(diào)用event queue, 但是如果存在未截獲的exception,app會退出。
立即加入event queue,使用new Future():
// Adds a task to the event queue.
new Future(() {
// ...code goes here...
});
等待一段時間后,再加入event queue,使用use new Future.delayed():
// After a one-second delay, adds a task to the event queue.
new Future.delayed(const Duration(seconds:1), () {
// ...code goes here...
});
當需要做動畫的時候,不要使用Future,而需要使用animateFrame
PS:
1.FutureTask執(zhí)行完成后,then()會立即執(zhí)行(then并沒有創(chuàng)建新的event丟到event 1ueue中,而只是Function Call);
2.當FutureTask在then()被調(diào)用之前執(zhí)行完成,則會創(chuàng)建一個task,并將該task的添加到microtask queue中;
3.Future只是創(chuàng)建了一個event,將event插入到了event queue的隊尾;
4.Future.value()跟第二條一樣,創(chuàng)建Task丟到microtask Queue中;
5.Future.sync()立即執(zhí)行傳入的function,如果function返回future,則會創(chuàng)建Task丟到microtask Queue中執(zhí)行。
Microtask queue: scheduleMicrotask()
使用scheduleMicrotask
在dart:async庫中定義了scheduleMicrotask()方法來使用microtask queue。
scheduleMicrotask(() {
// ...code goes here...
});
另外一種使用microtask queue的方式,是在Future中調(diào)用then()。
Use isolates or workers if necessary
當有計算很繁重的任務(wù)時,則需要使用isolate或者Worker來執(zhí)行,以保持App對用戶操作的及時響應(yīng)。Isolate可能是一個單獨的線程,或者一個單獨的進程,這取決于Dart的具體實現(xiàn)。通常情況下可以根據(jù)你的cpu的個數(shù)來決定isolate的數(shù)量。但你也可以使用超過cpu個數(shù)的isolate,前提是你有好的app架構(gòu),讓不同的isolate來分擔不同的代碼塊運行,并能保證這些isolate之間沒有數(shù)據(jù)共享。