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 ?
Asked
Active
Viewed 1,942 times
2 Answers
8
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

Community
- 1
- 1

Marcos Casagrande
- 37,983
- 8
- 84
- 98
-
But child process in node allows you to run terminal commands. Seems like the does not – SeanMC Jul 03 '20 at 19:02
-
1You can use Deno.run if you want to spawn shell commands. – Marcos Casagrande Jul 03 '20 at 20:01
-
1https://stackoverflow.com/questions/61710787/how-to-run-a-python-script-from-deno see my answer there – Marcos Casagrande Jul 03 '20 at 20:04
-
Very helpful, thank you. Am I able to send multiple commands and receive multiple responses for the same running process in Deno? I'm having a hard time finding examples. – SeanMC Jul 04 '20 at 00:32
-
In one of my examples I show how to write to `stdin`. You need to read from stdout and write to stdin. – Marcos Casagrande Jul 04 '20 at 09:47
0
You can use child processes. Here is an example: proc
with PushIterable
This will let you send and receive multiple commands from non-Deno child processes asynchronously, as well as Deno child processes.
Be careful as this requires --allow-run
to work, and this almost always breaks out of the sandbox, if you care about that.

j50n
- 1