I am trying to interact with a Python script from Node via stdin/out using child_process
like so:
var p = require('child_process').spawn('python', ['test_io.py']);
p.stdout.on('data', function(data) {
console.log(data.toString());
});
p.stdin.write('thing');
and this is the relevant Python portion:
import io
import sys
_input = io.open(sys.stdin.fileno())
_output = io.open(sys.stdout.fileno(), 'w')
while True:
_output.write(_input.readline())
However, right now it seems the Python script is not reading "thing" passed in via stdin.write
. Shouldn't these writes be buffering? What am I doing wrong here.
Thanks in advance.
try this
var p = require('child_process').spawn('python', ['test_io.py'], { stdio: 'pipe'});
ChildProcess has some idiosyncracies -- from the docs for ChildProcess.stdin:
If the child was not spawned with stdio[0] set to 'pipe', then this will not be set.
https://nodejs.org/api/child_process.html#child_process_child_stdin
In addition to @lispHK01 answer You need to terminate the stdin - particularly when sending small amounts of data as it will be sitting in a buffer.
Try this:
var p = require('child_process').spawn('python', ['test_io.py'], { stdio: 'pipe'});
p.stdin.write('thing');
p.stdin.end();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.