I'm trying to municate 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 munication in a chronolical way:
- recieve "data" from python
- send "go" to python
- recieve "data2" from python
Another Question: In the tutorial on 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)
I'm trying to municate 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 munication in a chronolical way:
- recieve "data" from python
- send "go" to python
- recieve "data2" from python
Another Question: In the tutorial on https://github./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)
Share Improve this question edited Jul 1, 2015 at 13:41 goetzmoritz asked Jul 1, 2015 at 13:32 goetzmoritzgoetzmoritz 4531 gold badge7 silver badges25 bronze badges1 Answer
Reset to default 5Your 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 mand 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 bee:
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);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790909a4593909.html
评论列表(0条)