【发布时间】:2020-09-17 00:01:47
【问题描述】:
假设我有 2 个脚本,father.ts 和 child.ts,我如何从father.ts 生成 child.ts 并定期从father.ts 向 child.ts 发送消息?
【问题讨论】:
标签: deno
假设我有 2 个脚本,father.ts 和 child.ts,我如何从father.ts 生成 child.ts 并定期从father.ts 向 child.ts 发送消息?
【问题讨论】:
标签: deno
您可以使用子进程。这是一个例子:proc with PushIterable
这将允许您从非 Deno 子进程以及 Deno 子进程异步发送和接收多个命令。
要小心,因为这需要--allow-run 才能工作,如果您关心的话,这几乎总是会突破沙盒。
【讨论】:
你必须使用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();
};
您可以使用.postMessage发送消息
【讨论】:
stdin。您需要从标准输出读取并写入标准输入。