如何生成子进程并与其进行通信Deno? [英] How to spawn child process and communicate with it Deno?
本文介绍了如何生成子进程并与其进行通信Deno?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有2个脚本,father.ts和child.ts,我该如何从father.ts产生child.ts并定期将信息从father.ts发送到child.ts?
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 ?
推荐答案
您必须使用 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
这篇关于如何生成子进程并与其进行通信Deno?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文