2

I have been trying to communicate between Node JS and Python following this tutorial: http://www.sohamkamani.com/blog/2015/08/21/python-nodejs-comm/

The python file reads a javascript array and prints the sum using the numpy module. Here is the code for both the python and node js file.

Python code:

 import sys, json, numpy as np
 #Read data from stdin
 def read_in():
 lines = sys.stdin.readlines()
 return json.loads(lines[0])
 def main():
 #get our data as an array from read_in()
 lines = read_in()
 #create a numpy array
 np_lines = np.array(lines)
 #use numpys sum method to find sum of all elements in the array
 lines_sum = np.sum(np_lines)
 #return the sum to the output stream
 print lines_sum
 #start process
 if __name__ == '__main__':
 main()

Node js code:

 var spawn = require('child_process').spawn,
 py =spawn('python', ['compute_input.py']),
 data = [1,2,3,4,5,6], 
 dataString='';
 py.stdout.on('data', function(data) {
 dataString += data.toString();
 });
 py.stdout.on('end', function() {
 console.log('Sum = ', dataString);
 });
 py.stdin.write(JSON.stringify(data));
 py.stdin.end();

Error message:

 Error: write EPIPE
 at exports._errnoException (util.js:1018:11)
 at Socket._writeGeneric (net.js:711:26)
 at Socket._write (net.js:730:8)
 at doWrite (_stream_writable.js:331:12)
 at writeOrBuffer (_stream_writable.js:317:5)
 at Socket.Writable.write (_stream_writable.js:243:11)
 at Socket.write (net.js:657:40) 
 at Module._compile (module.js:570:32)
 at Object.Module._extensions..js (module.js:579:10)
asked Aug 10, 2017 at 14:55
11
  • (1) This is not valid Python code. (2) How do you run the scripts? Commented Aug 10, 2017 at 14:58
  • (1) Can you clarify what is not valid? (2) I am running the scripts with the node js command prompt. Commented Aug 10, 2017 at 15:02
  • The function definitions like **def read_in():** should not contain these stars. If you run the Python script separately the interpreter would tell you. I guess node swallows the error message from the child process. Commented Aug 10, 2017 at 15:05
  • I understand that python functions do not use **. I wanted it to be clear where the functions were. Please see revised format. Commented Aug 10, 2017 at 15:12
  • It's clearer without them :) Have you tried running the Python script manually? There may be less obvious errors. Commented Aug 10, 2017 at 15:16

1 Answer 1

2

The Python script is throwing an error. The python command invoked python3 instead of python 2.7, for which the script was written and the script errored out. EPIPE error is thrown because the python script ended prematurely and node tries to read-from/write-to to an already closed pipe.

answered Aug 10, 2017 at 17:01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.