6

I've been following along with a couple examples I found to call a Python script from Node. I'm able to execute the script, but I can't return data from Python.

test.js

var sys   = require('sys'),
    spawn = require('child_process').spawn,
    dummy  = spawn('python', ['test.py']);

dummy.stdout.on('data', function (data) {
sys.print("testing...\n");
sys.print(data);
});

test.py

import time
def dummy() :
    out = '';
    for i in range(0,10) :
        out += str(i + 1) + "\n"
        time.sleep(0.1)
    print out
    return out
if __name__ =='__main__' :
    dummy = dummy()

Could someone provide an example of how to return the results from test.py to test.js?

Thanks.

Note: test.py edited for proper indent.

1
  • I think my question comes down to this: How do I either return a value from python's main() (if name == 'main'), or how do I call a specific python method (dummy()) from Node? Commented Dec 30, 2013 at 0:39

3 Answers 3

4

I figured out how to do this, by printing the data in Python, and "listening" for the print in Node using stdout.on().

test.js

var sys   = require('sys'),
    spawn = require('child_process').spawn,
    dummy  = spawn('python', ['test.py']);

dummy.stdout.on('data', function(data) {
    sys.print(data.toString());
});

test.py

import time
def dummy() :
    out = '';
    for i in range(0,10) :
        out += str(i + 1) + ", "
        time.sleep(0.1)
    print out
if __name__ =='__main__' :
    dummy = dummy()
Sign up to request clarification or add additional context in comments.

Comments

3

As the Sys is deprecated, we can use util. Please find the modified code using util

test.js

var sys   = require('util'),
    spawn = require('child_process').spawn,
    dummy  = spawn('python', ['test.py']);

dummy.stdout.on('data', function(data) {
    console.log(data.toString());
});

test.py

import time
def dummy() :
    out = '';
    for i in range(0,10) :
        out += str(i + 1) + ", "
        time.sleep(0.1)
    print out
if __name__ =='__main__' :
    dummy = dummy()

Comments

1

I think your test.py has bug. You should check again for the indents, then run python test.py in your terminal to see if it print 1 to 10 or not:

import time
def dummy() :
  out = '';
  for i in range(0,10) :
    out += str(i + 1) + "\n"
    time.sleep(0.1)
  print out
  return out

if __name__ =='__main__' :
    dummy = dummy()

You should redirect both stdout, stderr to parent process, that help you quickly identify the bug:

var spawn = require('child_process').spawn,
var dummy  = spawn('python', ['test.py']);

dummy.stdout.pipe(process.stdout);
dummy.stderr.pipe(process.stderr);

5 Comments

Thanks for the reply. I ran python test.py and it did print 1 - 10.
Did you get any message when run node test.js?
No, there are no errors or messages. Both test.js and test.py seem to complete normally.
as I see, the python code in your question has wrong indent. And if test.js is not print result, I think this line of code dummy.stderr.pipe(process.stderr); would print the error.
Oh, I see what happened. You are right, the indents in my post aren't correct. I went back and looked at the code in my editor, and it runs fine. I didn't indent properly after I copied/pasted for my original post. So, when I run the code as I posted above, I do get an error, as you say. When I run it as it is in my editor, with proper indents, I see 1 - 10 printed out. My apologies for the improper indents in my post.

Your Answer

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.