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

質問編集履歴

1

補足説明を加えました。

2018/12/24 13:12

投稿

donarudo
donarudo

スコア15

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
  Node.jsとPythonを連携させたシステムを作る。
3
3
  pythonshellというライブラリを使って、Node.jsからpythonを実行しようとしています
4
- そして、python上で求めた変数をNode.js上で表示させ思っています
4
+ そして、python上で求めた変数をNode.js上で表示させたいと思っています
5
5
  ### 発生している問題・エラーメッセージ
6
6
  参考サイトにあるサンプルは動作させることが出来たのですが、自分の実行させたいファイルを実行することが出来ません。
7
7
  そのプログラムはargparseを用いてテキストファイルを用いて入出力しているのですが、実行のところで詰まっています。
@@ -11,16 +11,108 @@
11
11
  エラーメッセージ
12
12
  ```
13
13
 
14
+ TypeError: self.stderrParser is not a function
15
+ at /Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:302:42
16
+ at Array.forEach (<anonymous>)
17
+ at PythonShell.recieveInternal (/Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:298:15)
18
+ at PythonShell.receiveStderr (/Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:282:21)
19
+ at Socket.<anonymous> (/Users/admin/Desktop/ws-v/node_js/node_modules/python-shell/index.js:100:18)
20
+ at emitOne (events.js:116:13)
21
+ at Socket.emit (events.js:211:7)
22
+ at addChunk (_stream_readable.js:263:12)
23
+ at readableAddChunk (_stream_readable.js:250:11)
24
+ at Socket.Readable.push (_stream_readable.js:208:10)
25
+ ```
14
- ### 該当のソースコード
26
+ Node.js
27
+ ```
28
+ var {PythonShell} = require('python-shell');
15
29
 
30
+ var shell = new PythonShell('pysawit.py',{
31
+ mode: 'run',
32
+ scriptPath: './',
33
+ args: ['-i',"/Users/admin/Desktop/ws-v/python/examples/ini.txt", '-o',"/Users/admin/Desktop/ws-v/python/examples/ini.txt",'-n',22]
34
+ });
35
+
36
+ // facade.pyからの返事を待機
37
+ shell.on('message', data => {
38
+ // データを表示
39
+ console.log(data.result);
40
+ });
41
+
42
+ // 入力を終了
16
- ```ここに言語名を入力
43
+ shell.end();
17
- ソースコード
44
+
18
45
  ```
46
+ Python
47
+ ```
48
+ def output_dailyrun(self, initialize=False):
49
+ """
50
+ Prepare the list of daily model output, then retrieve and store their results.
19
51
 
20
- ### 試したこと
52
+ # Arguments
53
+ initialize (bool): `True` to initialize the output dictionry, else
54
+ `False` to retrieve and store model output results in dictionary
21
55
 
22
- ここに問題に対して試したことを記載してください。
56
+ # Returns
57
+ None:
23
58
 
59
+ """
60
+ out = self.out
61
+ ops = self.model
62
+ if not initialize:
63
+ # 1. get results for daily properties:
64
+ ag_growth = ops.parts.pinnae.growth + ops.parts.rachis.growth + ops.parts.trunk.growth
65
+ veg_growth = ag_growth + ops.parts.roots.growth
66
+ out['age'] = ops.treeage
67
+ out['tmin'] = ops.daytmin
68
+ out['tmax'] = ops.daytmax
69
+ out['totalrad'], out['directrad'], out['diffuserad'] = ops.dayrad
70
+ out['wind'] = ops.daywind
71
+ out['rain'] = ops.dayrain
72
+ out['netrain'] = ops.netrain
73
+ out['ambientCO2'] = ops.co2ambient
24
- ### 補足情報(FW/ツールのバージョンなど)
74
+ out['LAI'] = ops.lai
75
+ out['pinnae'] = ops.parts.pinnae.weight
76
+ out['rachis'] = ops.parts.rachis.weight
77
+ out['trunk'] = ops.parts.trunk.weight
78
+ out['roots'] = ops.parts.roots.weight
79
+ out['male'] = ops.parts.maleflo.weight
80
+ out['female'] = ops.parts.femaflo.weight
81
+ out['bunches'] = ops.parts.bunches.weight
82
+ out['flowersex'] = ops.flowersex
83
+ out['VDM'] = ops.vdmwgt
84
+ out['TDM'] = ops.tdmwgt
85
+ out['assim_photosyn'] = ops.dayassim
86
+ out['assim_maint'] = ops.assim4maint
87
+ out['assim_growth'] = ops.assim4growth
88
+ out['assim_gen'] = ops.assim4gen
89
+ out['VDM_growth'] = ag_growth
90
+ out['TDM_growth'] = veg_growth
91
+ out['yield'] = ops.bunchyield
92
+ out['trunk_hgt'] = ops.trunkhgt
93
+ out['rootdepth'] = ops.rootdepth
94
+ out['rootzone_VWC'] = ops.rootwater.vwc
95
+ # out['rootzone_pF'] = ops.rootwater.pF
96
+ out['waterstress'] = ops.waterstresses.crop
97
+ out['actual_E'] = ops.aet.soil
98
+ out['actual_T'] = ops.aet.crop
99
+ out['pot_T'] = ops.dayet.crop
100
+ # out['runoff'] = ops.runoff
101
+ # add soil layers output to the list
102
+
103
+ # オブジェクトを作成
104
+ response = {"result": ops.rootwater.vwc}
105
+ # JSON文字列にして出力
106
+ print(json.dumps(response))
25
107
 
108
+ ```
109
+ pythonのプログラムは長いので省略しています。
110
+
111
+
112
+ ###試したこと
113
+ Node.jsのファイルとpythonのファイルは同じ階層にあって
114
+ python pysawit.py run -i "/Users/admin/Desktop/ws-v/node_js/python/examples/ini.txt" -o "/Users/admin/Desktop/ws-v/node_js/python/examples/output.txt" -n 22 で実行できます。
115
+ 要するに、上のスクリプトをNode.jsで実行してpythonで求めた変数をNode.jsに送信して表示させたいのです。
26
- ここにより詳細な情報を記載してださい。
116
+ 情報が少なて大変困ってます
117
+ 下手な質問で申し訳ありませんが、どうか、ご指摘の方よろしくお願いします。
118
+ もう少し簡単なやり方があれば教えていただけるとありがたいです。