Created
July 24, 2016 22:15
-
-
Save jkomyno/32a0f3dbf6cdf48ae453344caf6d7166 to your computer and use it in GitHub Desktop.
Basic NodeJS - Python CGI using inner module child_process
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
import sys, json, numpy as np | |
#Read data from stdin | |
def read_in(): | |
lines = sys.stdin.readlines() | |
#Since our input would only be having one line, parse our JSON data from that | |
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() |
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
var spawn = require('child_process').spawn, | |
py = spawn('python', ['compute_input.py']), | |
fakeInput = [1,2,3,4,5,6,7,8,9], | |
dataString = ''; | |
py.stdout.on('data', function(data){ | |
dataString += data.toString(); | |
}); | |
py.stdout.on('end', function(){ | |
console.log('Sum of numbers=',dataString); | |
}); | |
py.stdin.write(JSON.stringify(fakeInput)); | |
py.stdin.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment