D
Deno

help

deno command stdin, stdout continous reading

TTangJieHao2/13/2024
I am trying to spawn an AI (cpp code) however after launching the code it immediately exits. It also isn't providing any stdout either. relevant code:
async function load_kata_go(config) {
const command = new Deno.Command(config.EXE, {
args: ["analysis", "-model", config.MODEL, "-config", config.CONFIG],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});

const process = command.spawn();
return process;
}

async function main() {
const cli_kata_config = await load_cli_config();
const katago = await load_kata_go(cli_kata_config);

const result = await katago.output();
console.log(new TextDecoder().decode(result.stdout));

//command args
//command settings
//AI
//analyze games
}

main();
async function load_kata_go(config) {
const command = new Deno.Command(config.EXE, {
args: ["analysis", "-model", config.MODEL, "-config", config.CONFIG],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});

const process = command.spawn();
return process;
}

async function main() {
const cli_kata_config = await load_cli_config();
const katago = await load_kata_go(cli_kata_config);

const result = await katago.output();
console.log(new TextDecoder().decode(result.stdout));

//command args
//command settings
//AI
//analyze games
}

main();
Ppyrote2/13/2024
If I replace config.EXE with "echo" the Deno code behaves correctly and prints stdout. Also tried a mock bash script too so the issue seems to be with that exe. Have you tried printing result.stderr to see if the exe is outputting any error messages?
TTangJieHao2/13/2024
The issue for me was that the program wasn't persistent and it ended immediately. Even when I hard coded it. I got reading the stream to work.
async function readStream(stream, handler) {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = new TextDecoder().decode(value);
handler(text);
}
} catch (error) {
console.error('Stream reading error:', error);
} finally {
reader.releaseLock();
}
}
async function readStream(stream, handler) {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = new TextDecoder().decode(value);
handler(text);
}
} catch (error) {
console.error('Stream reading error:', error);
} finally {
reader.releaseLock();
}
}
And used this:
async function main() {
const cli_kata_config = await load_cli_config();
const katago = await load_kata_go(cli_kata_config);

// Setup continuous reading of stdout and stderr
readStream(katago.stdout, (text) => console.log('stdout:', text));
readStream(katago.stderr, (text) => console.error('stderr:', text));

const analysisRequest = {
//...
};
await gameAnalysis(game, katago)
await katago.stdin.close();

const status = await katago.status;
console.log('Subprocess exited with:', status.code);
}

main().catch(console.error);
async function main() {
const cli_kata_config = await load_cli_config();
const katago = await load_kata_go(cli_kata_config);

// Setup continuous reading of stdout and stderr
readStream(katago.stdout, (text) => console.log('stdout:', text));
readStream(katago.stderr, (text) => console.error('stderr:', text));

const analysisRequest = {
//...
};
await gameAnalysis(game, katago)
await katago.stdin.close();

const status = await katago.status;
console.log('Subprocess exited with:', status.code);
}

main().catch(console.error);
W/e the issue was. Katago doesn't exit immediately

Looking for more? Join the community!