質問編集履歴
3
自己解決
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -89,3 +89,163 @@
|
|
|
89
89
|
thread_pool = ThreadPoolExecutor(max_workers=5)
|
|
90
90
|
thread_pool.submit(testfunc)
|
|
91
91
|
```
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
### 自己解決
|
|
95
|
+
|
|
96
|
+
最小構成プログラムを作成して実行したところ、
|
|
97
|
+
c#側からスレッドプールで実施されている画像読み出しを行うことが出来ました
|
|
98
|
+
コードを記載いたします
|
|
99
|
+
お手数おかけして大変申し訳ありませんでした
|
|
100
|
+
|
|
101
|
+
実験コードでは上手くいったので、何が原因で問題が発生したのかを突き止めたいと思います
|
|
102
|
+
ご協力頂きありがとうございました
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
# -*- coding: utf-8 -*-
|
|
106
|
+
|
|
107
|
+
from PIL import Image
|
|
108
|
+
import cv2
|
|
109
|
+
import imageio
|
|
110
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
111
|
+
|
|
112
|
+
def testfunc():
|
|
113
|
+
path = 'C:/data/test.bmp'
|
|
114
|
+
print(path)
|
|
115
|
+
try:
|
|
116
|
+
img = imageio.v2.imread(path)
|
|
117
|
+
print('Image Read comp cv2')
|
|
118
|
+
|
|
119
|
+
img = cv2.imread(path)
|
|
120
|
+
print('Image Read comp cv2')
|
|
121
|
+
|
|
122
|
+
img = Image.open(path)
|
|
123
|
+
print('Image Read comp')
|
|
124
|
+
|
|
125
|
+
except Exception as e:
|
|
126
|
+
print(e)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if __name__ == "__main__":
|
|
130
|
+
|
|
131
|
+
print('Hello Python')
|
|
132
|
+
|
|
133
|
+
# Thread Poolを作成
|
|
134
|
+
thread_pool = ThreadPoolExecutor(max_workers=5)
|
|
135
|
+
thread_pool.submit(testfunc)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
print('Python Finish')
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
```c#
|
|
144
|
+
using System;
|
|
145
|
+
using System.Collections.Generic;
|
|
146
|
+
using System.Data;
|
|
147
|
+
using System.Diagnostics;
|
|
148
|
+
using System.Linq;
|
|
149
|
+
using System.Text;
|
|
150
|
+
using System.Threading.Tasks;
|
|
151
|
+
using System.Windows;
|
|
152
|
+
using System.Windows.Controls;
|
|
153
|
+
using System.Windows.Data;
|
|
154
|
+
using System.Windows.Documents;
|
|
155
|
+
using System.Windows.Input;
|
|
156
|
+
using System.Windows.Media;
|
|
157
|
+
using System.Windows.Media.Imaging;
|
|
158
|
+
using System.Windows.Navigation;
|
|
159
|
+
using System.Windows.Shapes;
|
|
160
|
+
|
|
161
|
+
namespace WpfApp1
|
|
162
|
+
{
|
|
163
|
+
/// <summary>
|
|
164
|
+
/// Interaction logic for MainWindow.xaml
|
|
165
|
+
/// </summary>
|
|
166
|
+
public partial class MainWindow : Window
|
|
167
|
+
{
|
|
168
|
+
|
|
169
|
+
private void OnExited_Train(object? sender, EventArgs e)
|
|
170
|
+
{
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// <summary>
|
|
174
|
+
/// イベントハンドラ 標準出力用
|
|
175
|
+
/// RedirectStandardOutput = True
|
|
176
|
+
/// OutputDataReceivedイベントハンドラ追加
|
|
177
|
+
/// </summary>
|
|
178
|
+
/// <param name="sender"></param>
|
|
179
|
+
/// <param name="e"></param>
|
|
180
|
+
private void OnDataReceived_Train(object sender, DataReceivedEventArgs e)
|
|
181
|
+
{
|
|
182
|
+
if (e.Data != null) {
|
|
183
|
+
Debug.WriteLine(e.Data);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/// <summary>
|
|
188
|
+
/// イベントハンドラ エラーメッセージ用
|
|
189
|
+
/// RedirectStandardError = True
|
|
190
|
+
/// ErrorDataReceivedイベントハンドラ追加
|
|
191
|
+
/// </summary>
|
|
192
|
+
/// <param name="sender"></param>
|
|
193
|
+
/// <param name="e"></param>
|
|
194
|
+
private void OnErrorReceived_Train(object sender, DataReceivedEventArgs e)
|
|
195
|
+
{
|
|
196
|
+
if (e.Data != null) {
|
|
197
|
+
Debug.WriteLine(e.Data);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public MainWindow()
|
|
202
|
+
{
|
|
203
|
+
InitializeComponent();
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
// Pythonインタープリタのパス
|
|
207
|
+
var python_InterpreterPath = $@"C:\Users\******\anaconda3\envs\tf2_10_0_mkl_py39\python.exe";
|
|
208
|
+
|
|
209
|
+
if (System.IO.File.Exists(python_InterpreterPath)) {
|
|
210
|
+
// Pythonスクリプトのパス
|
|
211
|
+
// .pyが存在する場所
|
|
212
|
+
var python_ScriptPath = "PythonApplication1.py";
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
// Pythonスクリプトに渡す引数
|
|
216
|
+
var arguments = new List<string>
|
|
217
|
+
{
|
|
218
|
+
python_ScriptPath,
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
var process = new Process() {
|
|
222
|
+
StartInfo = new ProcessStartInfo(python_InterpreterPath) {
|
|
223
|
+
UseShellExecute = false,
|
|
224
|
+
CreateNoWindow = true,
|
|
225
|
+
RedirectStandardOutput = true,
|
|
226
|
+
RedirectStandardError = true,
|
|
227
|
+
RedirectStandardInput = true,
|
|
228
|
+
|
|
229
|
+
Arguments = string.Join(" ", arguments),
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
process.OutputDataReceived += OnDataReceived_Train;
|
|
235
|
+
process.ErrorDataReceived += OnErrorReceived_Train;
|
|
236
|
+
process.EnableRaisingEvents = true;
|
|
237
|
+
process.Exited += new EventHandler(OnExited_Train);
|
|
238
|
+
|
|
239
|
+
// Process Start ----------
|
|
240
|
+
process.Start();
|
|
241
|
+
|
|
242
|
+
process.BeginOutputReadLine();
|
|
243
|
+
process.BeginErrorReadLine();
|
|
244
|
+
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
```
|
2
python側コード追記
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -66,4 +66,26 @@
|
|
|
66
66
|
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
実行は正常にされますがImage.open部で止まるような挙動となります
|
|
69
|
+
実行は正常にされますがImage.open部で止まるような挙動となります
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
### テスト環境追記
|
|
73
|
+
python側でメインスレッドでは問題無く読み出し出来ることが確認出来ました。
|
|
74
|
+
(状況をうまく伝えることが出来る申し訳ありません)
|
|
75
|
+
スレッドプールで上記を記載するとpython単体での実行は問題無いが、c#側からの読み出して固まることがわかりました
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
def testfunc():
|
|
79
|
+
path = 'C:/data/test.bmp'
|
|
80
|
+
try:
|
|
81
|
+
img = Image.open(path)
|
|
82
|
+
print('Image Read comp')
|
|
83
|
+
except Exception as e:
|
|
84
|
+
print(log + 'Error', e)
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
|
|
88
|
+
# Thread Poolを作成
|
|
89
|
+
thread_pool = ThreadPoolExecutor(max_workers=5)
|
|
90
|
+
thread_pool.submit(testfunc)
|
|
91
|
+
```
|
1
c#側コード追加
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -18,3 +18,52 @@
|
|
|
18
18
|
|
|
19
19
|
原因がわからず困り果てております。
|
|
20
20
|
何が問題なのが教えて頂けないでしょうか?
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### 追記
|
|
24
|
+
Anaconda仮想環境でpythonを構築し、その仮想環境のpythonを呼び出し
|
|
25
|
+
c#側からProcessでpython.pyを実行します
|
|
26
|
+
```c#
|
|
27
|
+
// Pythonインタープリタのパス
|
|
28
|
+
var python_InterpreterPath = $"{仮想環境パス}\\python.exe";
|
|
29
|
+
|
|
30
|
+
if (System.IO.File.Exists(python_InterpreterPath)) {
|
|
31
|
+
// Pythonスクリプトのパス
|
|
32
|
+
// .pyが存在する場所
|
|
33
|
+
var python_ScriptPath = "test.py";
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
// Pythonスクリプトに渡す引数
|
|
37
|
+
var arguments = new List<string>
|
|
38
|
+
{
|
|
39
|
+
python_ScriptPath ,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
var process = new Process() {
|
|
43
|
+
StartInfo = new ProcessStartInfo(python_ScriptPath) {
|
|
44
|
+
UseShellExecute = false,
|
|
45
|
+
CreateNoWindow = true,
|
|
46
|
+
RedirectStandardOutput = true,
|
|
47
|
+
RedirectStandardError = true,
|
|
48
|
+
RedirectStandardInput = true,
|
|
49
|
+
|
|
50
|
+
Arguments = string.Join(" ", arguments),
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
process .OutputDataReceived += OnDataReceived_Train;
|
|
56
|
+
process .ErrorDataReceived += OnErrorReceived_Train;
|
|
57
|
+
process .EnableRaisingEvents = true;
|
|
58
|
+
process .Exited += new EventHandler(OnExited_Train);
|
|
59
|
+
|
|
60
|
+
// Process Start ----------
|
|
61
|
+
process .Start();
|
|
62
|
+
|
|
63
|
+
process .BeginOutputReadLine();
|
|
64
|
+
process .BeginErrorReadLine();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
実行は正常にされますがImage.open部で止まるような挙動となります
|