You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sending message to python from node.js using python-shell without terminating the python script #260
i am doing some communication between electron.js/node.js and python. i can run a file from python and send a message to it, which is capture via stdin, and that python file can print my message to the electron console. but, this only seems to work if i terminate the program after sending my message in node whereas, i want send the message while the program is running.
var pyshell = new PythonShell('test.py');
pyshell.send(JSON.stringify([1,2,3,4,5]));
pyshell.on('message', function (message) {
console.log(message);
});
pyshell.end(function (err) {
if (err){
throw err;
};
console.log('finished');
});
import sys, json
#Read data from stdin
def read_in():
lines = sys.stdin.readlines()
return json.loads(lines[0])
def main():
lines = sys.stdin.readlines()
lines = json.loads(lines[0])
total_sum_inArray = 0
for item in lines:
total_sum_inArray += item
print (lines)
main()
my output is [1,2,3,4,5] as it should be, but when i remove pyshell.end listener in the node code, i don't get any output in the console. i'm not sure how sys and stdin exactly work so maybe that's why i cannot figure this out. also, this is my first time asking a question on github so a bit nervous hehe.
thanks for any guidance.
def main():
#get our data as an array from read_in()
lines = sys.stdin.readlines()
lines = json.loads(lines[0])
total_sum_inArray = 0
for item in lines:
total_sum_inArray += item
print (lines,flush=True) //modification here
and unfortunately, i still didn't see anything in the console while pyshell.end function was commented out. i will read print buffering though
I'm not at the computer at the moment so I can't test it but I suspect the
problem is that sys.stdin.readlines() blocks until stdin is closed. Try
reading the input in a different way.
isn't the only way to read input with pyshell is to use sys.stdin? and stdin will remain open until pyshell does pyshell.end which ends the script. so unless the script ends, communication won't work? sorry, just trying to wrap my head around this.
There are several ways of reading from stdin. For example you could use the
input() method which reads one line at a time. You could also iterate over
stdin.readlines in a loop.
Also note that the input method is built into python, you don't need any
imports to use it. The input method is what beginners usually use.
On Tue, Nov 23, 2021, 8:44 AM Caleb Sparks ***@***.***>
wrote:
so i made the following changes to the python and it worked!:
def main():
c = input()
print (c)
main()
print("finished")
now, i added the shell.on and shell.send functions inside an event listener that communicates with the front-end, and I found that the python script only runs once.
# Check if the code is ready by nodejs, And if it is, it will return ready in nodejs
isReady = input()
if(isReady.rstrip() == "check"):
print("ready")
while True:
c = input()
Message = c.rstrip()
# Do your thing here, and return it using print
print("Some results")
# Flush all the print statements from console
sys.stdout.flush()
time.sleep(0.1)
Activity
Almenon commentedon Nov 18, 2021
Try
print(lines, flush=True)Print output in python is buffered by default. I suggest looking up python print buffering and reading up on it.
8ahmedanwer8 commentedon Nov 18, 2021
this is what i had
and unfortunately, i still didn't see anything in the console while pyshell.end function was commented out. i will read print buffering though
Almenon commentedon Nov 20, 2021
8ahmedanwer8 commentedon Nov 22, 2021
isn't the only way to read input with pyshell is to use sys.stdin? and stdin will remain open until pyshell does pyshell.end which ends the script. so unless the script ends, communication won't work? sorry, just trying to wrap my head around this.
Almenon commentedon Nov 23, 2021
Almenon commentedon Nov 23, 2021
8ahmedanwer8 commentedon Nov 24, 2021
so i made the following changes to the python and it worked!:
now, i added the shell.on and shell.send functions inside an event listener that communicates with the front-end, and I found that the python script only runs once.
so above, i see my output from python, done message and then only done message logged after that when the event is fired by a button
KaSakee commentedon Mar 31, 2022
You might wanna try this: