我有一个Node.JS服务器,它使用Pyshell.send向Python脚本发送消息
当Pyshell使用print Node.JS返回消息时,将使用Pyshell.on接收该消息,然后读取返回的消息
当再次使用第二条消息执行此操作时,它会崩溃,并出现以下错误:
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:243:12)
at Socket.Writable.write (_stream_writable.js:291:5)
at PythonShell.send (/Users/maxkenney/Desktop/nodeLanguageProcessing/node_modules/python-shell/index.js:285:20)
at Namespace.<anonymous> (/Users/maxkenney/Desktop/nodeLanguageProcessing/app.js:87:10)
at Namespace.emit (events.js:189:13)
at Namespace.emit (/Users/maxkenney/Desktop/nodeLanguageProcessing/node_modules/socket.io/lib/namespace.js:213:10)
at /Users/maxkenney/Desktop/nodeLanguageProcessing/node_modules/socket.io/lib/namespace.js:181:14
at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
at writeAfterEnd (_stream_writable.js:245:10)
at Socket.Writable.write (_stream_writable.js:291:5)
[... lines matching original stack trace ...]
at process._tickCallback (internal/process/next_tick.js:61:11)
我相信这可能是因为Python外壳在一条消息后关闭了,但我不确定。任何帮助都将不胜感激
Node.JS脚本(相关部分)
let pyshell = new PythonShell('my_script.py');
pyshell.on('message', function(message){
console.log("python said: " + message);
});
pyshell.send("This is a test entry").end(function(err){
if(err) handleError(err);
});
最后一个pyshell.send稍后会重复,并崩溃
Python脚本:
import sys
import spacy
import nltk
nlp = spacy.load("en_core_web_sm");
def convertGeneratorObjectToListOfString(generatorObject):
list = []
for item in generatorObject:
list.append(str(item))
return list
def main():
#doc = nlp(sys.argv[1])
doc = nlp(sys.stdin.readline())
# Return the printed message to Node
jsonObjectToReturn = {}
for token in doc:
dependents = token.children
listOfChildrenTag = []
for d in dependents:
listOfChildrenTag.append(d.dep_)
jsonObjectToReturn[token.text] = {
"text": token.text,
"lemma": token.lemma_,
"tag": token.tag_,
"dep": token.dep_,
"dependencies": convertGeneratorObjectToListOfString(token.children),
"dependenciesTag": listOfChildrenTag
}
print(jsonObjectToReturn)
if __name__ == '__main__':
main()
我在这里包含了所有的python代码,因为我不确定,但我相信这个问题也可能是因为python脚本在主脚本完成后关闭。抱歉,我不熟悉运行常量python脚本
当您调用
.end()
时,您将关闭该流,使其无法再使用。因此,在处理完流之前不要调用.end()
。您可以调用.send()
,而无需调用.end()
如果还需要监视python脚本何时结束,则可以使用pyshell对象的
childProcess
属性相关问题 更多 >
编程相关推荐