Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
8ahmedanwer8 opened this issue Nov 17, 2021 · 8 comments
Labels

Comments

@8ahmedanwer8
Copy link

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.

@Almenon
Copy link
Collaborator

Almenon commented 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
Copy link
Author

8ahmedanwer8 commented Nov 18, 2021

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

@Almenon
Copy link
Collaborator

Almenon commented Nov 20, 2021 via email

@8ahmedanwer8
Copy link
Author

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
Copy link
Collaborator

Almenon commented Nov 23, 2021 via email

@Almenon
Copy link
Collaborator

Almenon commented Nov 23, 2021 via email

@8ahmedanwer8
Copy link
Author

8ahmedanwer8 commented Nov 24, 2021

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

@KaSakee
Copy link

KaSakee commented Mar 31, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants