大家好,我写了个小的 dart 工具,用来汇总温度 CSV,每个文件起一个 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,但什么都没抛,也没有变化。
让我困惑的是,我记得 main 里的循环会在所有 spawn 出去的 isolate 都结束后自己结束。所以我才用 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.