How to spawn child process and communicate with it Deno?

拥有回忆 提交于 2020-06-28 02:58:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!