Skip to content

Commit 4093d17

Browse files
committed
v0.16
- 프로그램 안전성 개선 => add mutex working (Thread lock)
1 parent f8766cf commit 4093d17

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

main.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def article_parse(self, dc_id, keyword, search_type, page=1, search_pos=''):
8181
global all_link, g_type
8282
try:
8383
url = f"https://gall.dcinside.com/{g_type}/lists/?id={dc_id}&page={page}&search_pos={search_pos}&s_type={search_type}&s_keyword={keyword}"
84-
#print(url);
84+
print(url);
8585
res = requests.get(url, headers=headers)
8686
soup = BeautifulSoup(res.text, "lxml")
8787

@@ -104,9 +104,6 @@ def article_parse(self, dc_id, keyword, search_type, page=1, search_pos=''):
104104
else:
105105
reply = "0"
106106

107-
if '/' in reply:
108-
reply = reply.split('/')[0]
109-
110107
nickname = element.select(".ub-writer")[0].text.strip()
111108
timestamp = element.select(".gall_date")[0].text
112109
refresh = element.select(".gall_count")[0].text
@@ -119,12 +116,14 @@ def article_parse(self, dc_id, keyword, search_type, page=1, search_pos=''):
119116
'refresh': refresh, 'recommend': recommend}
120117
self.QTableWidgetUpdate.emit(article_data) # 글 데이터 방출
121118

119+
122120
except Exception as e:
123121
# print(e)
124122
self.ThreadMessageEvent.emit('글을 가져오는 중 오류가 발생했습니다.')
125123

126124
def run(self):
127125
self.mutex.lock()
126+
128127
global running
129128

130129
search_pos = ''
@@ -134,20 +133,23 @@ def run(self):
134133
search_type = search_dict[self.parent.comboBox.currentText()]
135134

136135
idx = 0
137-
while running:
136+
while (True):
137+
if running == False:
138+
return
139+
138140
if idx > loop_count or search_pos == 'last':
139141
self.QLabelWidgetUpdate.emit('상태 : 검색 완료')
140142
self.ThreadMessageEvent.emit('작업이 완료되었습니다.')
141143
running = False
142144
break
143145

144146
page = self.parent.page_explorer(id, keyword, search_pos)
145-
#print(page)
147+
print(page)
146148

147149
if not page['start'] == 0: # 글이 있으면
148150

149151
for i in range(page['start'], page['end'] + 1):
150-
if not running:
152+
if running == False:
151153
return
152154

153155
self.QLabelWidgetUpdate.emit(f'상태 : {idx}/{loop_count} 탐색중...')
@@ -158,24 +160,20 @@ def run(self):
158160
if idx > loop_count or search_pos == 'last':
159161
break
160162

161-
# time.sleep(0.1) # 디시 서버를 위한 딜레이
162-
self.msleep(100) # ※주의 QThread에서 제공하는 sleep을 사용
163+
time.sleep(0.1) # 디시 서버를 위한 딜레이
163164

164165
self.QLabelWidgetUpdate.emit(f'상태 : {idx}/{loop_count} 탐색중...')
165166
idx += 1 # 글을 못찾고 넘어가도 + 1
166167

167168
search_pos = page['next_pos']
169+
168170
self.mutex.unlock()
169-
running = False
170171

171172
def stop(self):
172-
global running
173-
running = False
174173
self.working = False
175-
174+
self.mutex.unlock()
176175
self.quit()
177-
self.msleep(5000) #wait로 하면 안되고 msleep으로 해야 제대로 mutex lock이 작동하는듯 하다.
178-
#self.wait(5000) # 5000ms = 5s
176+
self.wait(5000) # 5000ms = 5s
179177

180178

181179
class SlotEvent:
@@ -185,10 +183,6 @@ def ThreadMessageEvent(self, n):
185183

186184
@pyqtSlot(dict)
187185
def QTableWidgetUpdate(self, data):
188-
#업데이트를 너무 자주하면 GUI에 제대로 반영이 안되는 것이 있음.
189-
#IO 성능을 포기하더라도 delay보단 print로 출력해서 주는게 더 낫다는 생각..
190-
print(data)
191-
192186
rowPosition = self.articleView.rowCount()
193187
self.articleView.insertRow(rowPosition)
194188

@@ -213,8 +207,6 @@ def QTableWidgetUpdate(self, data):
213207
item_recommend.setData(Qt.DisplayRole, int(data['recommend'])) # 숫자로 설정 (정렬을 위해)
214208
self.articleView.setItem(rowPosition, 6, item_recommend)
215209

216-
217-
218210
@pyqtSlot(str)
219211
def QLabelWidgetUpdate(self, data):
220212
self.txt_status.setText(data)
@@ -360,8 +352,8 @@ def page_explorer(self, dc_id, keyword, search_pos=''):
360352
def search(self): # 글검색
361353
global all_link, g_type, running
362354

363-
if running: # 이미 실행중이면
364-
dialog = QMessageBox.question(self, 'Message', '검색이 진행중입니다. 새로 검색을 시작하시겠습니까? (이전 검색을 중단하기 위해 프로그램이 잠시 멈출 수 있습니다.)',
355+
if running == True: # 이미 실행중이면
356+
dialog = QMessageBox.question(self, 'Message', '검색이 진행중입니다. 새로 검색을 시작하시겠습니까?',
365357
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
366358
if dialog == QMessageBox.Yes:
367359
running = False

0 commit comments

Comments
 (0)