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