I was going to put it on my blog because it was still under construction. I put it here for fear that I would forget how to solve the problem.
preface
Deno is the new backend runtime that node.js rearranges its alphabet. I used to use Node.js to write child process interaction and so on. Today, I had the idea to implement it in Deno to prepare for future projects, so I started experimenting.
There is no way to reach the hill
Deno’s call to the child process only leaves a built-in method deno.run, which feels a bit crude. I don’t know if it can achieve mutual communication. Look at the documentation first, how there are so many fewer options than Node.js, the in the mind somehow feel bad.
Let’s follow the examples in the documentation first
/** * subprocess.ts */
const fileNames = Deno.args;
const p = Deno.run({
cmd: [
"deno"."run"."--allow-read"."https://deno.land/[email protected]/examples/cat.ts". fileNames, ],stdout: "piped".stderr: "piped"});const { code } = await p.status();
// Reading the outputs closes their pipes
const rawOutput = await p.output();
const rawError = await p.stderrOutput();
if (code === 0) {
await Deno.stdout.write(rawOutput);
} else {
const errorString = new TextDecoder().decode(rawError);
console.log(errorString);
}
Deno.exit(code);
Copy the code
Here is the document to interact with the child process of the demo code, run with the –allow-run parameter to run properly.
This demo code can only display the output after the subroutine has finished running. This is not what I want. My subroutine will output the output while running, and can receive input from the parent process.
So I started a search trip. However, to my great disappointment, there was too little information in Deno. After searching for a long time, I used the same method to process the output message as in the document.
Another village
I kept poking around in the editor to see if I could find any useful clues, and sure enough I found the stdout property, but sadly it didn’t appear in the documentation! Is this to be understood by oneself? Thanks to Deno writing in Typescript, the prompts are complete! Deno and Node.js have the same author. Streamer is a good design in Node.js, so Deno should keep the Streamer operation. Streamer is a data stream read, so it is possible to get an output message while the child process is running.
After a bit of searching, I found that Deno had separated the Streamer operation from the run-time API and needed to find it in the deno.std library. Some searching finally led me to the solution I needed, which I wrote down
import { readStringDelim } from "https://deno.land/[email protected]/io/mod.ts";
const p = Deno.run({
cmd: ['./qplayer'.'--uri=file:///.. /test.mp3'.'--output=local'.'--silent'].stderr: 'piped'.stdout: 'piped'});for await (let line of readStringDelim(p.stderr, "\n")) {
console.log(line)
}
p.close();
Copy the code
The key here is the readStringDelim method, which returns a traversal object that can be used for await() for asynchronous processing, equivalent to on(‘data’) event processing in Node.js.
I have documented this solution here in the hope that it will be helpful to other students who have similar problems.