본문 바로가기
Dart

[Dart] Isolate 배우기

by 붕어사랑 티스토리 2021. 10. 18.
반응형

https://medium.com/dartlang/dart-asynchronous-programming-isolates-and-event-loops-bffc3e296a6a

 

Dart asynchronous programming: Isolates and event loops

Dart, despite being a single-threaded language, offers support for futures, streams, background work, and all the other things you need to…

medium.com

상기 내용을 기반으로 합니다.

 

 

Isolate

Dart는 싱글스레드 언어이다. 하지만 다른언어의 쓰레드 프로그래밍처럼 비동기 프로그래밍을 지원한다. 그게 바로 Isolate다

 

Isolate는 다음과 같은 특징을 가진다.

 

  • Isolate는 쓰레드와 다르게 메모리를 공유하지 않는다. Isolate마다 고유의 메모리공간을 가진다.
  • Isolate안에 event loop가 있어서 이벤트가 들어올때 마다 이를 처리하는 방식이다.
  • 하나의 작업을 여러개의 Isolate로 처리하고 싶을때 메세지를 이용하에 Isolate간 통신을 할 수있다.

Isolate의 동작그림. 내부에 event loop가 있어 이벤트를 처리해준다.

 

가령 main Isolate에 작업량이 많아 frame 드랍이 생길경우 Dart에서는 Isolate.spawn() 함수나 Flutter의 compute() 함수를 이용해 Isolate를 만들어 작업량을 분산시킬 수 있다.

 

Isolate.spawn(
  aFunctionToRun,
  {"data" : "here is some data"},
}


compute(
  (params) {
  /* 작업할 내용 */
  },
  {"data" : "here is some data"},
}

 

 

 

 

 

 

Isolate는 메세지를 통해 둘간에 통신을 할 수 있다

 

만약 하나의 작업을 두개 이상의 Isolate를 만들어 빠르게 처리하고 싶을 경우 메세지를 통하여 Isolate간 통신을 할 수 있다.

 

 

Event loop

event loop를 보여주는 그림

앞서 말한것 처럼 Isolate 내에서 event loop가 있고 event loop는 가장 오래된 event 를 처리한다.

 

앞서 Dart의 async 프로그래밍 - Future, Stream, async, await를 배웠다. 이 작업들은 사실 event queue에 event를 넣어주는 작업이다

반응형

댓글