python-shell icon indicating copy to clipboard operation
python-shell copied to clipboard

sending message to python from node.js using python-shell without terminating the python script

Open 8ahmedanwer8 opened this issue 3 years ago • 8 comments

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.

8ahmedanwer8 avatar Nov 17 '21 20:11 8ahmedanwer8

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.

Almenon avatar Nov 18 '21 16:11 Almenon

this is what i had

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

8ahmedanwer8 avatar Nov 18 '21 17:11 8ahmedanwer8

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.

On Thu, Nov 18, 2021, 9:12 AM 8ahmedanwer8 @.***> wrote:

this is what i had

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/extrabacon/python-shell/issues/260#issuecomment-973072149, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADDZTBMSSBIK2XXIOZYQBITUMUXY7ANCNFSM5IH7AUJQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Almenon avatar Nov 20 '21 20:11 Almenon

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.

8ahmedanwer8 avatar Nov 22 '21 19:11 8ahmedanwer8

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.

On Mon, Nov 22, 2021, 11:39 AM 8ahmedanwer8 @.***> wrote:

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.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/extrabacon/python-shell/issues/260#issuecomment-975857640, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADDZTBM7CZLVUHMWNGTAGZ3UNKL77ANCNFSM5IH7AUJQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Almenon avatar Nov 23 '21 16:11 Almenon

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:

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.

On Mon, Nov 22, 2021, 11:39 AM 8ahmedanwer8 @.***> wrote:

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.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/extrabacon/python-shell/issues/260#issuecomment-975857640, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADDZTBM7CZLVUHMWNGTAGZ3UNKL77ANCNFSM5IH7AUJQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Almenon avatar Nov 23 '21 16:11 Almenon

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.

ipcMain.on('getMetadata', function(event){
  shell2.send(JSON.stringify([1,2,3,4,60]));

  shell2.on('message', function (message) {
      console.log(message);
  });

  console.log("done")
})

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

8ahmedanwer8 avatar Nov 24 '21 02:11 8ahmedanwer8

You might wanna try this:

# 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)

KaSakee avatar Mar 31 '22 09:03 KaSakee