我正在学习将 electro js 与 python 一起使用,并且我正在使用蟒蛇壳 https://www.npmjs.com/package/python-shell所以我有以下简单的Python脚本:
import sys, json
# simple JSON echo script
for line in sys.stdin:
print(json.dumps(json.loads(line)))
在我的 main.js 中:
let {PythonShell} = require('python-shell')
let pyshell = new PythonShell('/home/bassel/electron_app/pyapp/name.py', {mode : 'json'});
pyshell.send({name:"mark"})
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
console.log("hi");
});
but the hi
没有打印出来,有什么问题吗?
This problem can also occur when trying to suppress the newline from the end of print
output. See Why doesn't print output show up immediately in the terminal when there is no newline at the end? https://stackoverflow.com/questions/25897335/.
通常对输出进行缓冲以保留系统资源。这意味着在这种情况下,系统会保留 Python 输出,直到有足够的输出可以一起释放。
为了克服这个问题,您可以显式“刷新”输出:
import sys, json
# simple JSON echo script
for line in sys.stdin:
print(json.dumps(json.loads(line)))
sys.stdout.flush() # <--- added line to flush output
如果您使用 Python 3.3 或更高版本,您也可以使用:
import sys, json
# simple JSON echo script
for line in sys.stdin:
print(json.dumps(json.loads(line)), flush=True) # <--- added keyword
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)