-
|
I am currently reading a large json doc from stdout to in memory gzip stream, and when cli is done read the gzip memory stream into I am trying to async read from stdin and async read json to avoid memory and latency overhead. Something like this: public async Task<(bool, string, List<FfMpegToolJsonSchema.Packet>)> GetPacketListAsync(
Command command
)
{
using MemoryStream memoryStream = new();
StringBuilder stdErrorBuffer = new();
CommandTask<CommandResult> task = command
.WithStandardOutputPipe(PipeTarget.ToStream(memoryStream, true))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrorBuffer))
.WithValidation(CommandResultValidation.None)
.ExecuteAsync(CancellationToken.None, Program.CancelToken());
CommandResult result = await task;
FfMpegToolJsonSchema.PacketInfo packetInfo =
await JsonSerializer.DeserializeAsync<FfMpegToolJsonSchema.PacketInfo>(
memoryStream,
ConfigFileJsonSchema.JsonReadOptions,
Program.CancelToken()
);
return (
task.Task.IsCompletedSuccessfully && result.ExitCode == 0,
stdErrorBuffer.ToString(),
packetInfo?.Packets
);
}This fails with an exception where the json deserializer did not find a json token, it looks like the json read does not wait for the stream to have data. What would a better / correct way be to use cliwrap to async pipe stdout to json? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
You'll have to create your own FfMpegToolJsonSchema.PacketInfo packetInfo;
var stdOutTarget = PipeTarget.Create(async (stdOutStream, cancellationToken) =>
{
packetInfo = await JsonSerializer.DeserializeAsync<FfMpegToolJsonSchema.PacketInfo>(
stdOutStream,
ConfigFileJsonSchema.JsonReadOptions,
cancellationToken
);
});
var cmd = command
.WithStandardOutputPipe(stdOutTarget)
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrorBuffer))
.WithValidation(CommandResultValidation.None);You can also just derive from You can refactor it to your liking, but that's the idea. |
Beta Was this translation helpful? Give feedback.
You'll have to create your own
PipeTarget. Rough sketch:You can also just derive from
PipeTagetand overrideCopyAsync(...)if you'd rather define your own type instead of using a one-off anonymous …