piping for async sub process
In Deno.process, is there a way to set its STDOUT to something I can read line by line while that primary process is still running?
12 Replies
Set stdout to piped and use the
readable
stream, I think?
https://doc.deno.land/deno/stable/~/Deno.run
https://doc.deno.land/deno/stable/~/Deno.ProcessDeno CLI APIs – Deno.run | Deno Doc
Spawns new subprocess. RunOptions must contain at a minimum the opt.cmd, an array of program arguments, the first of which is the binary.
const p = Deno.run({ cmd: ["curl", "https://example.com"], }); const status = await p.status();
Subprocess uses same working directory as parent process unless opt.cwd is specified.
Environmental variable...
Deno CLI APIs – Deno.Process | Deno Doc
Represents an instance of a sub process that is returned from {@linkcode Deno.run} which can be used to manage the sub-process.
Or stdin if you meant reading line by line inside the spawned process.
Thank you!
This works, but is VERY ugly
* Is there a better way to terminate the reading loop?
* Is there a better way to deal with the reads not filling the buffer?
https://deno.land/std@0.161.0/streams/mod.ts will probably have the helpers you want.
Also, TextDecoderStream exists:
https://deno.land/api@v1.26.2?s=TextDecoderStream
I read through the doc's and the sources (to the extent I can) and I just don't get it.
Clearly what I am doig is not right.
error: TS2339 [ERROR]: Property 'pipeThrough' does not exist on type 'Reader & Closer & { readable: ReadableStream<Uint8Array>; }'.
Can you point me at a simple example?
stdout.readable.pipeThrough
Thank you -- i will try it
Thank you!
This is much less ugly!
Is there also a way to make the control of the
while
loop less ugly?
I presume your end goal is not to just log the output?
You might consider implementing your eventual use case as a writable stream. But if you just need to get the output then yeah, a while loop doing awaits is pretty much the way. You can improve it a bit by doing:
thank you a, a "do while" was exactly what I was hoping for
indeed there is more work to do with the results, but I am trying to understands.
the basics.
when I tried
it waited for the process to finish and then gives me all the reads rapidly rather tthan giving to me as they become available
Humm...
The problem was with my environment, the iterator works fine.
Sorry to be stupid.