3

I'm trying to communicate between node.js and python-shell. I was able to recieve data from python-shell-object but when I try to send a message to the python-shell it crashes.

my app.js:

var PythonShell = require('python-shell');

var options = {
    scriptPath: '/home/pi/python'
};

var pyshell = new PythonShell('test.py', options, {
    mode: 'text'
});

pyshell.stdout.on('data', function(data) {
    pyshell.send('go');
    console.log(data);
});

pyshell.stdout.on('data2', function(data) {
    pyshell.send('OK');
    console.log(data);
});

pyshell.end(function(err) {
    if (err) throw err;
    console.log('End Script');
});

and my test.py:

import sys
print "data"
for line in sys.stdin:
    print "data2"

I basically want to have communication in a chronolical way:

  1. recieve "data" from python
  2. send "go" to python
  3. recieve "data2" from python

Another Question: In the tutorial on https://github.com/extrabacon/python-shell it is written that you have to write pyshell.on() to wait for data while in the source-code the author writes pyshell.stdout.on(). Why is that?

Thanks!!! (wrong indention in python corrected)

1 Answer 1

5

Your code exhibits some incorrect use of python-shell. Here below I have put together some notes. However, this is just what I primarily spot as mistakes so it will just rectify the use of the python-shell library but it might not necessarily remove all issues with your Python counterpart.


Incorrect use of stdout.on('data')

You appear to incorrectly utilize the event handler stdout.on. The handler takes "data" as argument denotes the event which happens when an output message is printed from Python script. This always be stdout.on('data') regardless what messages are printed.

This one is not valid:

pyshell.stdout.on('data2', function(data) { .... })

It should always be

pyshell.stdout.on('data', function(data) { .... })

When relay a message to Python, you should enclose the command with end

You should change from:

pyshell.send('OK');

To this:

pyshell.send('OK').end(function(err){
    if (err) handleError(err);
    else doWhatever();
})

Therefore, rectifying those two mistakes, you code should become:

pyshell.stdout.on('data', function(data) {
    if (data == 'data') 
        pyshell.send('go').end(fucntion(err){
            if (err) console.error(err);
            // ...
        });
    else if (data == 'data2')
        pyshell.send('OK').end(function(err){
            if (err) console.error(err); 
            // ...
        });
    console.log(data);
 });
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. If I run your code it hangs (no error, just hanging). Strange.
Interesting. Can you put console.log at the beginning of each function and see where the process stuck? I would guess the process runs and waits for a response from python.
I figuered out that the if-statements both don't work. I can 'console.log' the content of data correct as "data" but I can't compare them like 'if(data == "data")'. Also in python the 'for line in sys.stdin:' does seem to hang. I guess it just doesn't recieve any data and stucks.
I think the problem arise from the python script. What I am trying to achieve is some kind of an event-listener like a .on()-statement in javascript by writing "for line in sys.stdin:" in python. This does not work.
I think it's worth trying "print the input message from stdin" in python. (Print whatever it gets from stdin). So basically JavaScript should get the echo message back. This simplified process might help you address the part where the issue sits in.
|

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.