spawn static method
Implementation
static Future<PooledExecutor> spawn(
String modelDir, {
required int workers,
required int maxSeq,
required int maxChunk,
WeightFormat format = WeightFormat.f32,
List<String> keepF32 = const [],
}) async {
final replies = ReceivePort();
final pool = PooledExecutor._(
Lfm3Config.load(modelDir),
workers,
replies,
maxChunk,
);
final ready = Completer<void>();
var readyCount = 0;
replies.listen((message) {
switch (message) {
case (int worker, SendPort port, int vocabStart):
pool._ports[worker] = port;
pool._vocabStarts[worker] = vocabStart;
if (++readyCount == workers) ready.complete();
case (
int worker,
int seq,
Float32List? result,
int micros,
int requestLatency,
int sentAt,
):
final replyLatency = DateTime.now().microsecondsSinceEpoch - sentAt;
final st = pool.stats;
st
..requestLatencyMicros += requestLatency
..replyLatencyMicros += replyLatency;
if (requestLatency > st.worstRequestLatency) {
st.worstRequestLatency = requestLatency;
}
if (replyLatency > st.worstReplyLatency) {
st.worstReplyLatency = replyLatency;
}
pool._receive(worker, seq, result, micros);
case (int worker, String error):
final failure = StateError('worker $worker: $error');
if (!ready.isCompleted) ready.completeError(failure);
for (final pending in pool._pending.values) {
pending.completer.completeError(failure);
}
pool._pending.clear();
}
});
for (var w = 0; w < workers; w++) {
await Isolate.spawn(_workerMain, (
replies.sendPort,
modelDir,
w,
workers,
maxSeq,
maxChunk,
format,
keepF32,
));
}
await ready.future;
return pool;
}