>假设我有 2 个脚本,father.ts 和 child.ts,我如何从 father.ts 生成 child.ts 并定期从 father.ts 向 child.ts 发送消息?
您必须使用 Worker API
父亲
const worker = new Worker("./child.ts", { type: "module", deno: true });
worker.postMessage({ filename: "./log.txt" });
儿童网
self.onmessage = async (e) => {
const { filename } = e.data;
const text = await Deno.readTextFile(filename);
console.log(text);
self.close();
};
您可以使用.postMessage
发送消息
您可以使用子进程。下面是一个示例:proc
PushIterable
这将允许您异步发送和接收来自非 Deno 子进程以及 Deno 子进程的多个命令。
要小心,因为这需要--allow-run
才能工作,如果你关心这一点,这几乎总是会脱离沙盒。