質問編集履歴

9

変更

2023/04/19 07:55

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -76,7 +76,9 @@
76
76
  print(e, type(e))
77
77
  else:
78
78
  if data == "SELECT":
79
- select()
79
+ threading.Thread(target=select, daemon=True).start()
80
+ elif data == "CANCEL":
81
+ threading.Thread(target=cancel, daemon=True).start()
80
82
 
81
83
  time.sleep(0.1)
82
84
 
@@ -92,14 +94,15 @@
92
94
  except Exception as e:
93
95
  print(e, type(e))
94
96
  else:
95
- if data == "CANCEL":
96
- cancel()
97
+ pass
97
98
 
98
99
  time.sleep(0.1)
99
100
 
100
101
 
101
102
  def select():
102
103
  global con, cur
104
+
105
+ print("select()")
103
106
 
104
107
  try:
105
108
  with psycopg2.connect(
@@ -130,6 +133,8 @@
130
133
  def cancel():
131
134
  global con
132
135
 
136
+ print("cancel()")
137
+
133
138
  if con:
134
139
  con.cancel()
135
140
 
@@ -141,9 +146,10 @@
141
146
 
142
147
 
143
148
  def on_button2():
144
- global gval2
149
+ global gval1
145
150
 
146
- gval2.put("CANCEL")
151
+ gval1.put("CANCEL")
152
+
147
153
 
148
154
  def on_close():
149
155
  global monitor_flag1, monitor_flag2, con

8

追記

2023/04/17 06:56

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -52,6 +52,7 @@
52
52
  メモ
53
53
 
54
54
  psycopg2のキャンセルサンプル。
55
+ ボタン1を押すと、SELECTクエリ実行。ボタン2を押すと、クエリ中断。
55
56
  (これを画面遷移と組み合わせるのが課題)
56
57
  ```Python
57
58
  import queue

7

修正

2023/04/17 06:45

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -54,6 +54,7 @@
54
54
  psycopg2のキャンセルサンプル。
55
55
  (これを画面遷移と組み合わせるのが課題)
56
56
  ```Python
57
+ import queue
57
58
  import threading
58
59
  import time
59
60
  import tkinter as tk
@@ -66,30 +67,34 @@
66
67
  global monitor_flag1, gval1
67
68
 
68
69
  while monitor_flag1:
70
+ try:
71
+ data = gval1.get_nowait()
72
+ except queue.Empty as e:
73
+ pass
74
+ except Exception as e:
75
+ print(e, type(e))
76
+ else:
77
+ if data == "SELECT":
78
+ select()
79
+
69
80
  time.sleep(0.1)
70
-
71
- if not gval1:
72
- continue
73
-
74
- data = gval1.pop()
75
-
76
- if data == "SELECT":
77
- select()
78
81
 
79
82
 
80
83
  def monitor_variable2():
81
84
  global monitor_flag2, gval2
82
85
 
83
86
  while monitor_flag2:
87
+ try:
88
+ data = gval2.get_nowait()
89
+ except queue.Empty as e:
90
+ pass
91
+ except Exception as e:
92
+ print(e, type(e))
93
+ else:
94
+ if data == "CANCEL":
95
+ cancel()
96
+
84
97
  time.sleep(0.1)
85
-
86
- if not gval2:
87
- continue
88
-
89
- data = gval2.pop()
90
-
91
- if data == "CANCEL":
92
- cancel()
93
98
 
94
99
 
95
100
  def select():
@@ -131,14 +136,13 @@
131
136
  def on_button1():
132
137
  global gval1
133
138
 
134
- gval1.append("SELECT")
139
+ gval1.put("SELECT")
135
140
 
136
141
 
137
142
  def on_button2():
138
143
  global gval2
139
144
 
140
- gval2.append("CANCEL")
145
+ gval2.put("CANCEL")
141
-
142
146
 
143
147
  def on_close():
144
148
  global monitor_flag1, monitor_flag2, con
@@ -154,8 +158,8 @@
154
158
 
155
159
 
156
160
  if __name__ == "__main__":
157
- gval1 = []
161
+ gval1 = queue.Queue()
158
- gval2 = []
162
+ gval2 = queue.Queue()
159
163
  monitor_flag1= True
160
164
  monitor_flag2= True
161
165
  con = None

6

修正

2023/04/17 06:26

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -110,6 +110,9 @@
110
110
  results = cur.fetchall()
111
111
  print("rows: {}".format(len(results)))
112
112
 
113
+ except psycopg2.errors.QueryCanceled as e:
114
+ print("cancel")
115
+
113
116
  except Exception as e:
114
117
  print(type(e), e)
115
118
 
@@ -121,7 +124,6 @@
121
124
  def cancel():
122
125
  global con
123
126
 
124
- print("cancel")
125
127
  if con:
126
128
  con.cancel()
127
129
 

5

修正

2023/04/17 04:05

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -137,6 +137,7 @@
137
137
 
138
138
  gval2.append("CANCEL")
139
139
 
140
+
140
141
  def on_close():
141
142
  global monitor_flag1, monitor_flag2, con
142
143
 

4

修正

2023/04/17 04:04

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -51,7 +51,8 @@
51
51
  ### 追記
52
52
  メモ
53
53
 
54
- psycopg2のキャンセルサンプル
54
+ psycopg2のキャンセルサンプル
55
+ (これを画面遷移と組み合わせるのが課題)
55
56
  ```Python
56
57
  import threading
57
58
  import time
@@ -61,83 +62,106 @@
61
62
  import psycopg2
62
63
 
63
64
 
64
- def monitor_variable():
65
+ def monitor_variable1():
65
- global monitor_flag, gval
66
+ global monitor_flag1, gval1
66
67
 
67
- while monitor_flag:
68
+ while monitor_flag1:
68
69
  time.sleep(0.1)
69
70
 
70
- if not gval:
71
+ if not gval1:
71
72
  continue
72
73
 
73
- data = gval.pop()
74
+ data = gval1.pop()
74
75
 
75
76
  if data == "SELECT":
76
- func()
77
+ select()
77
78
 
78
79
 
80
+ def monitor_variable2():
81
+ global monitor_flag2, gval2
82
+
83
+ while monitor_flag2:
84
+ time.sleep(0.1)
85
+
86
+ if not gval2:
87
+ continue
88
+
89
+ data = gval2.pop()
90
+
91
+ if data == "CANCEL":
92
+ cancel()
93
+
94
+
79
- def func():
95
+ def select():
80
96
  global con, cur
81
97
 
82
98
  try:
83
- con = psycopg2.connect(
99
+ with psycopg2.connect(
84
100
  host = "192.168.0.100",
85
101
  user = "user01",
86
102
  password = "pass01",
87
103
  dbname = "testdb"
88
- )
104
+ ) as con:
89
105
 
90
- cur = con.cursor()
106
+ with con.cursor() as cur:
91
-
92
- print("Query...")
107
+ print("SQL...")
93
- sql = "SELECT * FROM testview;"
108
+ sql = "SELECT * FROM testview;"
94
- cur.execute(sql)
109
+ cur.execute(sql)
95
- results = cur.fetchall()
110
+ results = cur.fetchall()
96
- print("rows: {}".format(len(results)))
111
+ print("rows: {}".format(len(results)))
97
-
98
- cur = None
99
- con = None
100
-
101
- cur.close()
102
- con.close()
103
112
 
104
113
  except Exception as e:
105
114
  print(type(e), e)
115
+
116
+ finally:
117
+ con = None
106
118
  cur = None
119
+
120
+
121
+ def cancel():
107
- con = None
122
+ global con
123
+
124
+ print("cancel")
125
+ if con:
126
+ con.cancel()
108
127
 
109
128
 
110
129
  def on_button1():
111
- global gval
130
+ global gval1
112
131
 
113
- gval.append("SELECT")
132
+ gval1.append("SELECT")
114
133
 
115
134
 
116
135
  def on_button2():
117
- global con
136
+ global gval2
118
137
 
119
- if con:
120
- con.cancel()
138
+ gval2.append("CANCEL")
121
139
 
122
140
  def on_close():
123
- global monitor_flag, con
141
+ global monitor_flag1, monitor_flag2, con
124
142
 
125
143
  if con:
126
144
  return
127
145
 
128
- monitor_flag = False
146
+ monitor_flag1 = False
147
+ monitor_flag2 = False
129
- thread.join()
148
+ thread1.join()
149
+ thread2.join()
130
150
  root.destroy()
131
151
 
132
152
 
133
153
  if __name__ == "__main__":
134
- gval = []
154
+ gval1 = []
155
+ gval2 = []
135
- monitor_flag = True
156
+ monitor_flag1= True
157
+ monitor_flag2= True
136
158
  con = None
137
159
  cur = None
138
160
 
139
- thread = threading.Thread(target=monitor_variable)
161
+ thread1 = threading.Thread(target=monitor_variable1)
162
+ thread2 = threading.Thread(target=monitor_variable2)
140
- thread.start()
163
+ thread1.start()
164
+ thread2.start()
141
165
 
142
166
  root = tk.Tk()
143
167
 

3

修正

2023/04/17 02:59

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -90,7 +90,7 @@
90
90
  cur = con.cursor()
91
91
 
92
92
  print("Query...")
93
- sql = "SELECT * FROM view_direction_plan;"
93
+ sql = "SELECT * FROM testview;"
94
94
  cur.execute(sql)
95
95
  results = cur.fetchall()
96
96
  print("rows: {}".format(len(results)))

2

追記

2023/04/17 02:59

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -48,3 +48,106 @@
48
48
 
49
49
  ただ、上のコードではDB操作を試みるとアプリが固まってボタンの操作もできなくなるので、[過去の質問](https://teratail.com/questions/296608)の回答にある実装方法に変えるつもりでいます。(GUIを動かすメインスレッドとは別のスレッドをガン回しする)
50
50
 
51
+ ### 追記
52
+ メモ
53
+
54
+ psycopg2のキャンセルサンプル
55
+ ```Python
56
+ import threading
57
+ import time
58
+ import tkinter as tk
59
+ from tkinter import ttk
60
+
61
+ import psycopg2
62
+
63
+
64
+ def monitor_variable():
65
+ global monitor_flag, gval
66
+
67
+ while monitor_flag:
68
+ time.sleep(0.1)
69
+
70
+ if not gval:
71
+ continue
72
+
73
+ data = gval.pop()
74
+
75
+ if data == "SELECT":
76
+ func()
77
+
78
+
79
+ def func():
80
+ global con, cur
81
+
82
+ try:
83
+ con = psycopg2.connect(
84
+ host = "192.168.0.100",
85
+ user = "user01",
86
+ password = "pass01",
87
+ dbname = "testdb"
88
+ )
89
+
90
+ cur = con.cursor()
91
+
92
+ print("Query...")
93
+ sql = "SELECT * FROM view_direction_plan;"
94
+ cur.execute(sql)
95
+ results = cur.fetchall()
96
+ print("rows: {}".format(len(results)))
97
+
98
+ cur = None
99
+ con = None
100
+
101
+ cur.close()
102
+ con.close()
103
+
104
+ except Exception as e:
105
+ print(type(e), e)
106
+ cur = None
107
+ con = None
108
+
109
+
110
+ def on_button1():
111
+ global gval
112
+
113
+ gval.append("SELECT")
114
+
115
+
116
+ def on_button2():
117
+ global con
118
+
119
+ if con:
120
+ con.cancel()
121
+
122
+ def on_close():
123
+ global monitor_flag, con
124
+
125
+ if con:
126
+ return
127
+
128
+ monitor_flag = False
129
+ thread.join()
130
+ root.destroy()
131
+
132
+
133
+ if __name__ == "__main__":
134
+ gval = []
135
+ monitor_flag = True
136
+ con = None
137
+ cur = None
138
+
139
+ thread = threading.Thread(target=monitor_variable)
140
+ thread.start()
141
+
142
+ root = tk.Tk()
143
+
144
+ button1 = ttk.Button(root, text="Button1", command=on_button1)
145
+ button1.grid()
146
+
147
+ button2 = ttk.Button(root, text="Button2", command=on_button2)
148
+ button2.grid()
149
+
150
+ root.protocol("WM_DELETE_WINDOW", on_close)
151
+ root.mainloop()
152
+ ```
153
+

1

修正

2023/04/16 16:11

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -46,5 +46,5 @@
46
46
  root.mainloop()
47
47
  ```
48
48
 
49
- ただ、上のコードではDB操作を試みるとアプリが固まってボタンの操作もできなくなるので、[過去の質問](https://teratail.com/questions/296608)の実装方法に変えるつもりでいます。
49
+ ただ、上のコードではDB操作を試みるとアプリが固まってボタンの操作もできなくなるので、[過去の質問](https://teratail.com/questions/296608)の回答にある実装方法に変えるつもりでいます。(GUIを動かすメインスレッドとは別のスレッドをガン回しする)
50
50