node.jsのpython-shellを使用して、pythonを起動し、
数秒後にコマンドをpythonに送信し、pythonを停止するプログラムをテストしています。
sendを実行の際にエラーが発生しました。ネット上で調査しても解消方法がわからず困っています。
sendは、python実行前の引数にしか使用できないのでしょうか?
プログラムは以下の通りです。
node.js
1var options = { 2 pythonPath: 'python.exeがある場所' , 3 pythonOptions: ['-u'], 4 scriptPath: '実行するpythonファイルがある場所' 5}; 6var pyshell = new PythonShell('実行するpythonファイル', options); 7 8pyshell.on('message', function (data) { 9 console.log("message:" + data); 10}); 11 12setTimeout(() => { 13 pyshell.send("q"); 14}, 10000); 15 16pyshell.end(function(err,code,signal) { 17 if (err) { 18 console.log("end error"); 19 console.log(err); 20 throw err; 21 } 22 console.log('The exit code was: ' + code); 23 console.log('The exit signal was: ' + signal); 24 console.log('finished'); 25 }); 26
python
1import sys 2 3while True: 4 readdata = sys.stdin.readline() 5 if readdata : 6 print(readdata) 7 if readdata == "q": 8 break
error
1events.js:288 2 throw er; // Unhandled 'error' event 3 ^ 4 5Error [ERR_STREAM_WRITE_AFTER_END]: write after end 6 at writeAfterEnd (_stream_writable.js:264:14) 7 at Socket.Writable.write (_stream_writable.js:313:5) 8 at PythonShell.send (\node_modules\python-shell\index.js:285:20) 9 at Timeout._onTimeout () 10 at listOnTimeout (internal/timers.js:549:17) 11 at processTimers (internal/timers.js:492:7) 12Emitted 'error' event on Socket instance at: 13 at errorOrDestroy (internal/streams/destroy.js:108:12) 14 at writeAfterEnd (_stream_writable.js:266:3) 15 at Socket.Writable.write (_stream_writable.js:313:5) 16 [... lines matching original stack trace ...] 17 at processTimers (internal/timers.js:492:7) { 18 code: 'ERR_STREAM_WRITE_AFTER_END'

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。