回答編集履歴
1
追記
test
CHANGED
@@ -17,3 +17,38 @@
|
|
17
17
|
- 実行中に停止の指示があれば、処理を中断して適切に終了する造りにする。
|
18
18
|
- 終了したら親プロセスに伝えるようにする。
|
19
19
|
|
20
|
+
|
21
|
+
---
|
22
|
+
追記
|
23
|
+
|
24
|
+
threadのちょっとした例です。
|
25
|
+
global変数stopをメインプロセスとスレッドで共有していて、メインで変更するとスレッドで検出できて止まるようになっています。
|
26
|
+
|
27
|
+
|
28
|
+
```python
|
29
|
+
import threading
|
30
|
+
import time
|
31
|
+
|
32
|
+
stop = False
|
33
|
+
|
34
|
+
|
35
|
+
def func():
|
36
|
+
global stop
|
37
|
+
while True:
|
38
|
+
if stop:
|
39
|
+
break
|
40
|
+
print('thread runnnig.')
|
41
|
+
time.sleep(3)
|
42
|
+
print('stopping')
|
43
|
+
|
44
|
+
|
45
|
+
if __name__ == "__main__":
|
46
|
+
|
47
|
+
th = threading.Thread(target=func)
|
48
|
+
th.start()
|
49
|
+
print("press return to stop")
|
50
|
+
input()
|
51
|
+
stop = True
|
52
|
+
th.join()
|
53
|
+
```
|
54
|
+
|