public async Task <BuildJobResult> FinishAsync(CancellationToken cancellationToken) { var inputByteArrays = await Task.WhenAll(_inputs.Select(async pair => new KeyValuePair <int, ArraySegment <byte> >(pair.Key, await pair.Value.GetBytesAsync(cancellationToken)))); using (var ctx = new JobContext()) { foreach (var pair in inputByteArrays) { ctx.AddInputBytesPinned(pair.Key, pair.Value); } foreach (var outId in _outputs.Keys) { ctx.AddOutputBuffer(outId); } //TODO: Use a Semaphore to limit concurrency; and move work to threadpool var response = ctx.Execute(new { framewise = ToFramewise() }); foreach (var pair in _outputs) { using (var stream = ctx.GetOutputBuffer(pair.Key)) { await pair.Value.CopyFromStreamAsync(stream, cancellationToken); } } return(BuildJobResult.From(response, _outputs)); } }
internal async Task <BuildJobResult> FinishAsync(JobExecutionOptions executionOptions, SecurityOptions securityOptions, CancellationToken cancellationToken) { var inputByteArrays = await Task.WhenAll(_inputs.Select(async pair => new KeyValuePair <int, ArraySegment <byte> >(pair.Key, await pair.Value.GetBytesAsync(cancellationToken)))); using (var ctx = new JobContext()) { foreach (var pair in inputByteArrays) { ctx.AddInputBytesPinned(pair.Key, pair.Value); } foreach (var outId in _outputs.Keys) { ctx.AddOutputBuffer(outId); } //TODO: Use a Semaphore to limit concurrency var message = new { security = securityOptions?.ToImageflowDynamic(), framewise = ToFramewise() }; var response = executionOptions.OffloadCpuToThreadPool ? await Task.Run(() => ctx.Execute(message), cancellationToken) : ctx.Execute(message); using (response) { foreach (var pair in _outputs) { using (var stream = ctx.GetOutputBuffer(pair.Key)) { await pair.Value.CopyFromStreamAsync(stream, cancellationToken); } } return(BuildJobResult.From(response, _outputs)); } } }