-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiThread.py
73 lines (55 loc) · 1.84 KB
/
MultiThread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
grid = QGridLayout()
self.setLayout(grid)
self.setWindowTitle("멀티 스레드 예제")
self.startGetDataBtn = QPushButton()
self.startGetDataBtn.setText("크롤링 시작하기")
self.startGetDataBtn.clicked.connect(self.onGetDataBtnClicked)
grid.addWidget(self.startGetDataBtn,0,0)
self.th = QThread()
self.worker = Worker()
self.worker.moveToThread(self.th)
self.worker.getDatacompleted.connect(self.onGetDataCompleted)
self.setGeometry(100, 50, 300, 300)
self.windowToCenter()
self.show()
@pyqtSlot()
def onGetDataBtnClicked(self):
print("Start Thread")
self.worker.run()
self.th.working = True
# 창을 가운데로 이동시키는 메서드입니다.
def windowToCenter(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
@pyqtSlot(int)
def onGetDataCompleted(self,data):
print("onGetDataCompleted. data : ".format(data))
class Worker(QObject):
getDatacompleted = pyqtSignal(int)
data = "Hi"
def __init__(self, parent=None):
super().__init__()
print("Itit")
self.parent = parent
self.working = True
def __del__(self):
print(".... end thread.....")
self.wait()
def run(self):
print("Thread run!")
data = {"foo":"bar"}
print("self.data : {}".format(self.data))
self.getDatacompleted.emit(1)
self.working = False
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MainWindow()
app.exec_()