質問するログイン新規登録
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

3009閲覧

node.jsのpython-shellを使用して、pythonを起動後、sendで送信するとエラーがでるため、解消方法を知りたい。

huckepain

総合スコア14

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/04/17 00:53

編集2020/04/17 01:00

0

0

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'

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

以下の部分が、影響しており、end処理が終わってからsend処理はできないことがわかりました。

nodejs

1 2pyshell.end(function(err,code,signal) { 3 if (err) { 4 console.log("end error"); 5 console.log(err); 6 throw err; 7 } 8 console.log('The exit code was: ' + code); 9 console.log('The exit signal was: ' + signal); 10 console.log('finished'); 11 });

なくせば、データ送信ができました。
また、inputで待機状態になってしまうので、
別スレッドにしました。

投稿2020/04/17 02:44

huckepain

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問