PythonShell in node (nwjs)

删除回忆录丶 提交于 2019-12-20 15:23:08

问题


I am trying to create a nw.js application that communicates with Python using the node module PythonShell.

The issue I am having is that nothing is written to the console unless I close stdin. However I would like to keep the stream open so that I can send multiple commands to the Python script, and have Python save its state.

Here are my scripts:

script.py

import sys

def main():
    command = sys.stdin.readlines()  # unused for now
    sys.stdout.write("HELLO WORLD")
    sys.stdout.flush()

if __name__ == '__main__':
    main()

main.js

var PythonShell = require('python-shell');
var pyshell = new PythonShell('script.py');

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

pyshell.send('hello');

At this point, nothing happens.

If I do pyshell.end(), then HELLO WORLD is output to the console. But then I am unable issue further pyshell.send commands.

How can I keep the Python child process running and waiting for input, yet pipe all output back to JS?


回答1:


A couple of problems:

  • Use sys.stdin.readline() instead of sys.stdin.readlines(). Otherwise, Python will continue to wait for you to finish the input stream. You should be able to send a ^D signal to terminate the end of the input, but that didn't work for me.

  • To keep the stream open, wrap the command line input in a loop (see Python code below)

Also important:

  • Input automatically appends \n, but output does not. For whatever reason, output needs both \n and sys.stdout.flush() to work; one or the other won't cut it.

  • Python-shell seems to cache your Python code. So if you make any changes to your Python file, you must restart the nwjs application for it to take effect.

Here is the full sample code that works:

script.py

import sys

def main():
    while True:
        command = sys.stdin.readline()
        command = command.split('\n')[0]
        if command == "hello":
            sys.stdout.write("You said hello!\n")
        elif command == "goodbye":
            sys.stdout.write("You said goodbye!\n")
        else:
            sys.stdout.write("Sorry, I didn't understand that.\n")
        sys.stdout.flush()

if __name__ == '__main__':
    main()

main.js

var PythonShell = require('python-shell');
var pyshell = new PythonShell('script.py');

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

pyshell.send('hello');

Now use pyshell.send("hello"), pyshell.send("goodbye"), or pyshell.send("garbage") and receive an immediate response in the JS console!



来源:https://stackoverflow.com/questions/42462072/pythonshell-in-node-nwjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!