Created
March 23, 2024 20:21
-
-
Save guest271314/9309337be390860c04abcbb2bfc92d6e to your computer and use it in GitHub Desktop.
Reading child process using node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node --experimental-default-type=module read_child_process.js | |
import { spawn } from "node:child_process"; | |
import { Duplex } from "node:stream"; | |
const [command, args] = ["python3", ["./stream.py"]]; | |
const { stdout, stderr } = spawn(command, args); | |
const { readable, writable } = Duplex.toWeb(stdout); | |
console.log(readable, writable); | |
/* | |
const data = await new Response(readable).text(); | |
console.log(data); | |
*/ | |
await readable.pipeTo( | |
new WritableStream({ | |
write(value) { | |
console.log(value); | |
}, | |
close() { | |
console.log(`${command} pipe closed.`); | |
} | |
}) | |
).catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S python3 -u | |
from random import choice | |
from string import digits | |
import itertools | |
import os | |
import sys | |
for chunk in iter(lambda: ''.join(choice(digits) for i in range(512)).encode(), b''): | |
if chunk is not None: | |
sys.stdout.buffer.write(chunk) | |
sys.stdout.buffer.flush() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment