Node.JS Pyshell在向Python发送第二条消息时崩溃,[ERR\u STREAM\u WRITE\u AFTER\u END]:WRITE AFTER END
我有一个Node.JS服务器,它使用Pyshell.send向Python脚本发送消息 当Pyshell使用print Node.JS返回消息时,将使用Pyshell.on接收该消息,然后读取返回的消息 当再次使用第二条消息执行此操作时,它会崩溃,并出现以下错误:Node.JS Pyshell在向Python发送第二条消息时崩溃,[ERR\u STREAM\u WRITE\u AFTER\u END]:WRITE AFTER END,python,node.js,Python,Node.js,我有一个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.W
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)
最后一个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脚本。当您调用时。请关闭流,使其无法再使用。所以,不要打电话,直到你处理完流为止。你可以打电话。不打电话就发送。结束
如果还需要监视python脚本何时结束,则可以使用pyshell对象的childProcess属性
let pyshell = new PythonShell('my_script.py');
pyshell.childProcess.on('exit', (code) => {
console.log(`python program ended with exit code: ${code}`);
});
哇,我真的很尴尬,事后看来我应该发现的,谢谢。但是,我按照您的建议进行了更改,错误仍然存在,因此我只能想象还有其他东西正在关闭该服务器stream@MaxKenney-您的python脚本是否已自行结束?你可以监视它何时从你的node.js程序中退出,并将其时间与你收到错误时的时间进行比较。我在google上做了一个搜索,但我似乎找不到任何关于监视python脚本何时结束的内容。这是pyshell内置的吗?对不起,我是很多人的新手this@MaxKenney-查看我在回答的末尾添加的内容。'Python程序以退出代码结束;发送数据后的“0”
let pyshell = new PythonShell('my_script.py');
pyshell.childProcess.on('exit', (code) => {
console.log(`python program ended with exit code: ${code}`);
});