Deno 如何生成子进程并与之通信?

Won*_*Hau 7 deno

假设我有 2 个脚本,father.ts 和 child.ts,我如何从father.ts 生成child.ts 并定期从father.ts 向child.ts 发送消息?

Mar*_*nde 7

你必须使用Worker API

父亲.ts

const worker = new Worker("./child.ts", { type: "module", deno: true });
worker.postMessage({ filename: "./log.txt" });
Run Code Online (Sandbox Code Playgroud)

child.ts

self.onmessage = async (e) => {
  const { filename } = e.data;
  const text = await Deno.readTextFile(filename);
  console.log(text);
  self.close();
};
Run Code Online (Sandbox Code Playgroud)

您可以使用发送消息 .postMessage