-
[스크랩/메모] flutter 쓰레드, isolate, 이벤트루프flutter 2021. 11. 29. 15:04
https://dunkey2615.tistory.com/227
[Flutter] 안드로이드에 비유한 개념 정리 - 비동기 UI 1 -
개인적인 공부를 위한 필사한 글입니다. Flutter에서 runOnUiThread()와 동일한 것은? Dart는 단일-스레드 실행 모델을 가지고 있고, Isolate(Dart 코드를 다른 스데르에서 실행하는 방법)와 이벤트 루프, 비
dunkey2615.tistory.com
https://benghak.github.io/2020-01-27-Flutter_week_05_01/
(Flutter/Dart) Async, Await, Future 비동기 다루기
The Complete 2020 Flutter Development Bootcamp with Dart - created by Angela Yu
Posted on January 27, 2020이 포스트는 DSC HUFS에서 진행하는 Flutter 스터디를 바탕으로 정리한 내용입니다.
플러터에서 async, await, future로 비동기 다루기
Dart는 싱글 쓰레드 기반의 언어입니다. 하지만 Dart로 만들어진 Flutter에서는 비동기를 다룹니다. 어떻게 플러터가 싱글 쓰레드로 비동기 처리를 하는지 알아봤더니 event loop를 쓴다고 합니다. 놀랍지 않은게 Android, iOS에서도 이런 loop(aka. main loop)를 사용한다고 합니다. Javascript에서도 event loop를 사용합니다. 다음은 event loop를 Flutter 팀에서 설명한 영상합니다.
Event loop를 이해하기 전에 isolate의 개념의 설명이 필요할 것 같아서 isolate 설명을 먼저 정리했습니다.
Isolate 의미
쓰레드는 자신만의 isolate와 메모리를 갖습니다. 각 isolate는 자신만의 메모리와 event loop를 할당 받습니다. isolate는 새로운 ioslate을 만들 수 있고, 새로운 isolate는 새로운 메모리와 새로운 event loop를 할당받습니다. 여기서 새로운 isolate는 부모 isolate의 메모리에 접근할 수 없습니다. 이렇게 isolate는 서로 격리(isolated)되어있다는 특징을 갖습니다. 따라서 서로 메모리를 공유하지 않고, 메모리 Locking이 필요없다는 장점이 있습니다. 격리된 각 isolate 간의 통신을 하려고 하면 메시지를 주고 받아야합니다.
Event loop 설명
“An event loop is a background infinite loop which periodically wakes up and looks in the event queue for any tasks that need to run. If any exist, the event loops puts them onto the run stack if and only if the CPU is idle.”
출처: https://medium.com/flutter-community/futures-async-await-threading-in-flutter-baeeab1c1fe3
이벤트 루프는 이벤트 큐에 들어있는 태스크들을 차례로 실행하는 루프입니다. 여기서 말하는 태스크는 I/O나 사용자가 dart에서 작성하는 코드로 인해 발생하는 모든 이벤트들입니다.
https://yoonhg84.tistory.com/345
Flutter 4일 차 - Flutter for iOS devs (4/8), Threading & asynchronicity
Threading & asynchronicity https://flutter.dev/docs/get-started/flutter-for/ios-devs#threading--asynchronicity How do I write asynchronous code? Dart 는 Isolates, an event loop, 비동기 프로그램을 지..
yoonhg84.tistory.com
https://stackoverflow.com/questions/56906744/what-thread-isolate-does-flutter-run-io-operations-on
What thread / isolate does flutter run IO operations on?
In flutter when using the http package or doing general IO operations for example import 'package:http/http.dart' as http; http.Response response = await http.get(url); if (response.statusCode =...
stackoverflow.com
Flutter 4일 차 - Flutter for iOS devs (4/8), Threading & asynchronicity
초프 초프(초보 프로그래머) 2020. 7. 30. 00:29Threading & asynchronicity
https://flutter.dev/docs/get-started/flutter-for/ios-devs#threading--asynchronicity
How do I write asynchronous code?
Dart 는 Isolates, an event loop, 비동기 프로그램을 지원하는 싱글 스레드 모델이 있다.
Isloate 를 생성하지 않는 한 dart code 는 메인 스레드에서 실행되고 event loop 에 의해 구동된다.
Flutter 의 event loop 은 iOS main loop 와 동일하다.
Dart 의 싱글 스레드 모델은 모든 것을 UI 멈춤을 초래하는 블러킹 연산으로 실행하는 것이 아니다.
그 대신 비동기 작업을 실행하는 async/await 와 같은 Dart 가 제공하는 비동기 기능을 사용한다.
How do you move work to a background thread?
Flutter 는 다일 스레드이고 event loop 를 실행하기 때문에 스레드 관리나 spawning background thread 를 걱정할 필요 없다.
디스크 접근, 네트워크 작업 등의 I/O 작업을 하고 있다면 안전하게 async/await 를 사용하고 끝낼 수 있다.
CPU 를 많이 점유하는 계산이 많은 작업이 필요할 때 event loop blocking 을 피하기 위해서 Isolate 로 옮길 수 있다.
I/O 작업을 위해서는 함수에 async 함수라고 정의해라 그리고 함수 안에 오래 걸리는 작업에 await 해라
많은 데이터를 처리하는 경우 async, await 를 써도 UI 를 멈추는 경우들이 있다.
Flutter 에서는 오랜 시간 작업이나 많은 처리량을 Isolate 를 사용하여 위해 멀티 코어 장점을 취할 수 있다.
Isolate 들은 실행 스레드가 분리되어 있다. main 실행과 heap 메모리를 포함한 어떤 메모리도 공유하지 않는다.
메인 스레드의 변수들에 접근할 수 없고 setState() 를 호출하여 UI 를 업데이트할 수 없다.
Isolate 는 이름처럼 메모리를 공유하지 않는다.
How do I make network requests?
https://flutter.dev/docs/get-started/flutter-for/ios-devs#how-do-i-make-network-requests
인기 많은 http package 를 사용한다면 Flutter 에서 네트워크 콜을 만드는 것은 쉽다.
일반적으로 개발하는 네트워킹의 많은 부분을 네트워크 콜을 쉽게 만들 수 있도록 추상화한 것이다.
How do I show the progress of a long-running task?
일반적으로 iOS 에서는 오래 걸리는 백그라운드 작업을 실행할 때 UIProgressView 를 사용한다.
플루터에서는 ProgressIndicator 위젯을 사용한다.
boolean 값으로 렌더링 되는 시점을 제어하는 방식으로 progress 를 보여줄 수 있다.
큰 작업이 시작하기 전에 상태를 업데이트하도록 지시하고 끝난 후에는 숨기도록 지시하라.
리스트를 만드는 추천, 효과, 효율적인 방법은 ListView.Builder 를 사용하는 것이다.
이것은 많은 양의 리스트, 동적 리스트를 만들때 좋다.
ListView 를 만드는 대신에 목록 초기 길이, ItemBuilder 함수로 ListView.builder 를 만들어라.
ItemBuilder 함수는 위치를 받고 위치에 맞는 cell 을 리턴하는 iOS table/collection view 의 cellForItemAt delegate 메서드와 유사하다.
What is the equivalent of a ScrollView in Flutter?
iOS 에서는 ScrollView 안에 view 들을 넣음으로써 스크롤 가능하게 한다.
Flutter 에서는 ListView 위젯을 사용하는 것이 가장 쉬운 방법이다.
수직으로 위젯을 배치할 수 있어서 iOS TableView, ScrollView 둘의 역할을 한다.
좋아요공감공유하기글 요소
출처: https://yoonhg84.tistory.com/345 [안녕하세요. 초보프로그래머를 줄여 초프입니다.]
출처: https://yoonhg84.tistory.com/345 [안녕하세요. 초보프로그래머를 줄여 초프입니다.]Flutter 4일 차 - Flutter for iOS devs (4/8), Threading & asynchronicity
Threading & asynchronicity https://flutter.dev/docs/get-started/flutter-for/ios-devs#threading--asynchronicity How do I write asynchronous code? Dart 는 Isolates, an event loop, 비동기 프로그램을 지..
yoonhg84.tistory.com
'flutter' 카테고리의 다른 글
copyWith 함수 (0) 2021.12.07 Stream과 Future (0) 2021.12.01 dart mixin 클래스를 이용한 검증 로직 분리 (0) 2021.11.23 [메모] flutter 단위테스트, 테스트코드 관련 (0) 2021.11.20 [메모] flutter const, final (0) 2021.11.17