问题
Suppose I have 2 script, father.ts and child.ts, how do I spawn child.ts from father.ts and periodically send message from father.ts to child.ts ?
回答1:
You have to use the Worker API
father.ts
const worker = new Worker("./child.ts", { type: "module", deno: true });
worker.postMessage({ filename: "./log.txt" });
child.ts
self.onmessage = async (e) => {
const { filename } = e.data;
const text = await Deno.readTextFile(filename);
console.log(text);
self.close();
};
You can send messages using .postMessage
来源:https://stackoverflow.com/questions/62085588/how-to-spawn-child-process-and-communicate-with-it-deno