ProPuke
ProPuke
DDeno
Created by ProPuke on 5/4/2024 in #help
Piping stdout from a ChildProcess while it's running
hmm.. I guess making deno emulate/present as a TTY so that programs buffer less is a different question. Thanks, you've addressed everything here!
9 replies
DDeno
Created by ProPuke on 5/4/2024 in #help
Piping stdout from a ChildProcess while it's running
Right you are. That does fix it. However, this is not necessary when running tasks in the terminal, which does display each line separately. Is it possible to duplicate this behaviour in Deno?
9 replies
DDeno
Created by ProPuke on 5/4/2024 in #help
Piping stdout from a ChildProcess while it's running
A simpler example would also be:
const process = new Deno.Command('./test', { stdout: 'piped' }).spawn();

for await (const data of process.stdout.values()){
console.log(`read ${data.length} bytes: ${data}`);
}
const process = new Deno.Command('./test', { stdout: 'piped' }).spawn();

for await (const data of process.stdout.values()){
console.log(`read ${data.length} bytes: ${data}`);
}
This also waits 3 seconds, and then reads all 4 bytes at once:
read 4 bytes: 49,10,50,10
read 4 bytes: 49,10,50,10
In all cases nothing from stdout is readable until the program has terminated.
9 replies
DDeno
Created by ProPuke on 5/4/2024 in #help
Piping stdout from a ChildProcess while it's running
So the following program prints "1\n", waits 3 seconds, then prints "2\n":
#include <stdio.h>
#include <unistd.h>

int main() {
printf("1\n");
sleep(3);
printf("2\n");
return 0;
}
#include <stdio.h>
#include <unistd.h>

int main() {
printf("1\n");
sleep(3);
printf("2\n");
return 0;
}
But if I run the following in deno I get no output for 3 seconds, then all 4 bytes at once when the program terminates:
const process = new Deno.Command('./test', { stdout: 'piped' }).spawn();

(async() => {
const reader = process.stdout.getReader();
while(true){
const read = await reader.read();
if(read.value){
console.log(`read ${read.value.length} bytes`);
}
if(read.done){
console.log('read complete');
break;
}
}
})();

const status = await process.status;
console.log(`process finished with code ${status.code}`);
const process = new Deno.Command('./test', { stdout: 'piped' }).spawn();

(async() => {
const reader = process.stdout.getReader();
while(true){
const read = await reader.read();
if(read.value){
console.log(`read ${read.value.length} bytes`);
}
if(read.done){
console.log('read complete');
break;
}
}
})();

const status = await process.status;
console.log(`process finished with code ${status.code}`);
read 4 bytes
read complete
process finished with code 0
read 4 bytes
read complete
process finished with code 0
9 replies