Dart異步

《The Event Loop and Dart》譯文

原文:https://webdev.dartlang.org/articles/performance/event-loop

Event loops and queues

event loop循環的從event queue中取出一個event并執行。

event-loop.png

event queue中的event可以是用戶輸入、文件IO、定時器等等:

event-loop-example.png

Dart’s single thread of execution

一旦一個dart function開始執行,就會繼續執行下去直到它運行結束,換句話說,一條dart語句不能被其他的dart語句打斷。

注意: A Dart command-line app 可以在不同的isolate上平行的執行代碼。(Dart web app 不能直接創建額外的isolate,但可以創建worker) isolate相互隔離、不共享內存、通過傳遞消息來完成之間的通信。 除顯示調用方法將dart語句放在isolate或者worker以外,dart語句全部運行在主線程。更多的信息可以參考 Use isolates or workers if necessary 。

Dart APP當在主isolate中調用完main()方法后,就按順序循環執行event queue中的event,如下圖:

event-loop-and-main.png

Dart’s event loop and queues

Dart APP存在一個單獨的event loop和兩個event queue(event queue 和 microtask queue)。

Microtask Queue存在的意義是在處理一個event時,部分語句稍后執行,但希望在下一個event之前處理完畢。
例如,當一個狀態發生改變時,它將若干變化組合一起同步報告。microtask queue使得在DOM在顯示狀態變化前,狀態可發生多次變化。

event queue包含了dart自定義event、系統event,但microtask queue只包含了自定義event,但我們期望Web實現包含瀏覽器microtask queue。

both-queues.png

注意:當event looper正在處理microtask queue中的Event時候,event queue中的event就停止了處理了,此時App不能繪制任何圖形,不能處理任何鼠標點擊,不能處理文件IO等等

Event-Looper優先執行Microtask Queue中的Event,直到Microtask Queue為空時,才會執行Event Queue中的Event

Dart中只能知道Event處理的先后順序,但是并不知道某個Event執行的具體時間點,因為它的處理模型是一個單線程循環,而不是基于時鐘調度(即它的執行只是按照Event處理完,就開始循環下一個Event,而與Java中的Thread調度不一樣,沒有時間調度的概念),也就是我們既是指定另一個Delay Time的Task,希望它在預期的時間后開始執行,它有可能不會在那個時間執行,需要看是否前面的Event是否已經Dequeue。

鏈式調用明確語句順序

如果語句之間存在依賴關系,那就明確依賴關系。明確的依賴關系讓別人能更容易理解。
下面是一種錯誤的用法:

// BAD because of no explicit dependency between setting and using
// the variable.
future.then(...set an important variable...);
Timer.run(() {...use the important variable...});

應該替換為:

// BETTER because the dependency is explicit.
future.then(...set an important variable...)
    .then((_) {...use the important variable...});

好的做法是用then()去明確變量設置和使用的前后順序。(如果不管依賴是否正確執行,就要執行后續的語句,可以使用whenComplete()去代替when())
如果變量設置是異步操作,建議將變量使用語句放在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

當有代碼需要在后續任務執行的時候,可以通過dart:async提供的兩種方法實現:

1.使用Future類,可以將任務加入到Event Queue的隊尾
2.使用scheduleMicrotask函數,將任務加入到Microtask Queue隊尾

Use the appropriate queue (usually: the event queue)

盡可能使用event queue,microtask queue會阻塞event queue的執行。
如果語句需要在event queue中其他event之前執行,可以通過scheduleMicrotask()添加到microtask queue。

scheduling-tasks.png

Event queue: new Future()

可以通過new Future() 或者 new Future.delayed()來使用event queue。

注意:也可以使用Timer來調用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執行完成后,then()會立即執行(then并沒有創建新的event丟到event 1ueue中,而只是Function Call);
2.當FutureTask在then()被調用之前執行完成,則會創建一個task,并將該task的添加到microtask queue中;
3.Future只是創建了一個event,將event插入到了event queue的隊尾;
4.Future.value()跟第二條一樣,創建Task丟到microtask Queue中;
5.Future.sync()立即執行傳入的function,如果function返回future,則會創建Task丟到microtask Queue中執行。

Microtask queue: scheduleMicrotask()

使用scheduleMicrotask
在dart:async庫中定義了scheduleMicrotask()方法來使用microtask queue。

scheduleMicrotask(() {
    // ...code goes here...
});

另外一種使用microtask queue的方式,是在Future中調用then()。

Use isolates or workers if necessary

當有計算很繁重的任務時,則需要使用isolate或者Worker來執行,以保持App對用戶操作的及時響應。Isolate可能是一個單獨的線程,或者一個單獨的進程,這取決于Dart的具體實現。通常情況下可以根據你的cpu的個數來決定isolate的數量。但你也可以使用超過cpu個數的isolate,前提是你有好的app架構,讓不同的isolate來分擔不同的代碼塊運行,并能保證這些isolate之間沒有數據共享。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容