안녕하세요, 온도 CSV를 요약해 주는 작은 dart 툴을 만들고 있어요. 큰 파일들이 서로 기다리지 않도록 파일마다 isolate를 하나씩 띄워요. WSL에서 이렇게 실행해요:
dart run bin/summarize.dart data/north.csv data/south.csv -v
-v를 주면 "spawned worker" 줄이 두 번 나오고 그다음에 "got ... rows" 줄이 두 번 나와요. 행 수도 맞고요. 그러니까 worker들은 분명히 일을 하고 있어요. 그런데 그다음엔 아무것도 없어요. 표도 안 나오고, 프롬프트도 안 돌아와서 Ctrl+C를 눌러야 해요.
처음엔 worker 안의 readAsLinesSync가 뭔가를 막고 있나 했고, 그다음엔 worker 본문을 try/catch로 감싸 봤는데, 던져지는 것도 없고 달라지는 것도 없어요.
헷갈리는 건, 띄운 isolate가 전부 끝나면 main의 루프도 알아서 끝났던 걸로 기억한다는 거예요. 그래서 결과 개수를 세지 않고 await for를 쓰고 있어요. 주요 부분은 이래요:
final port = ReceivePort();
for (final p in paths) {
await Isolate.spawn(summarizeFile, WorkOrder(p, port.sendPort));
log.debug('spawned worker for $p');
}
final results = <FileSummary>[];
await for (final msg in port) {
final s = msg as FileSummary;
results.add(s);
log.info('got ${s.path}: ${s.count} rows');
}
results.sort((a, b) => a.path.compareTo(b.path));
printTable(results);
worker는 마지막에 order.replyTo.send(FileSummary(...))만 하고 끝나요. 최근 Dart 3.x예요. 그 루프가 끝나는 시점에 뭔가 바뀐 게 있나요? 이게 왜 이런지 이해하기 전에는 결과를 모으는 부분을 다시 짜고 싶지 않아요.
영어에서 번역·원문 보기hi all, small dart tool here, it summarises temperature CSVs and I spawn one isolate per file so the big ones don't wait for each other. i run it in WSL like this:
dart run bin/summarize.dart data/north.csv data/south.csv -v
with -v i see the two "spawned worker" lines and then two "got ... rows" lines, and the row counts are right. so the workers clearly do their work. but then nothing. no table, no prompt back, i have to Ctrl+C.
first i thought the readAsLinesSync in the worker was blocking something, then i wrapped the worker body in try/catch, but nothing is thrown and nothing changes.
what confuses me is that i remember the loop in main ending by itself once all the spawned isolates were finished. that's why i'm using await for and not counting results. this is the main part:
final port = ReceivePort();
for (final p in paths) {
await Isolate.spawn(summarizeFile, WorkOrder(p, port.sendPort));
log.debug('spawned worker for $p');
}
final results = <FileSummary>[];
await for (final msg in port) {
final s = msg as FileSummary;
results.add(s);
log.info('got ${s.path}: ${s.count} rows');
}
results.sort((a, b) => a.path.compareTo(b.path));
printTable(results);
worker just does order.replyTo.send(FileSummary(...)) at the end and returns. recent Dart 3.x. did something change about when that loop ends? i don't want to rewrite the collecting part before i understand this.