节点中的PythonShell(nwjs) [英] PythonShell in node (nwjs)

查看:114
本文介绍了节点中的PythonShell(nwjs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个nw.js应用程序,该应用程序使用节点模块PythonShell与Python进行通信.

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

我遇到的问题是,除非关闭stdin,否则什么都不会写入控制台.但是,我想保持流打开,以便可以向Python脚本发送多个命令,并让Python保存其状态.

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.

这是我的脚本:

script.py

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');

这时什么也没发生.

如果我执行pyshell.end(),则HELLO WORLD将输出到控制台.但是后来我无法再发出pyshell.send命令.

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

如何使Python子进程保持运行并等待输入,而又将所有输出通过管道传递回JS?

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

推荐答案

几个问题:

  • 使用sys.stdin.readline()代替sys.stdin.readlines().否则,Python将继续等待您完成输入流.您应该能够发送^D信号来终止输入的结尾,但这对我不起作用.

  • 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.

要保持流打开,请将命令行输入包装在一个循环中(请参见下面的Python代码)

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

也很重要:

  • 输入自动附加\n,但输出不附加.无论出于何种原因,输出都需要\nsys.stdout.flush()起作用;一个或另一个不会削减它.

  • 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似乎可以缓存您的Python代码.因此,如果您对Python文件进行了任何更改,则必须重新启动nwjs应用程序才能使其生效.

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

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');

现在使用pyshell.send("hello")pyshell.send("goodbye")pyshell.send("garbage")并在JS控制台中立即收到响应!

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

这篇关于节点中的PythonShell(nwjs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆