-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·53 lines (40 loc) · 1.44 KB
/
console.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
import logging, sys
from PyQt4.QtCore import QCoreApplication, QObject, Qt, QThread, QTimer, pyqtSignal, pyqtSlot
class MTWorker(QObject):
start = pyqtSignal()
finished = pyqtSignal()
def __init__(self):
super(MTWorker, self).__init__()
self.start.connect(self.doWork)
@pyqtSlot()
def doWork(self):
logging.getLogger().info('doWork slot triggered, doing some work!')
self.finished.emit()
if __name__ == '__main__':
# set up logger so we can see the thread activity
logging.basicConfig(format='%(threadName)s: %(message)s', level=logging.DEBUG)
logger = logging.getLogger()
app = QCoreApplication(sys.argv)
t = QThread()
w = MTWorker()
heart = QTimer()
def check_pulse():
if not t.isRunning():
heart.stop()
app.quit()
# exit the application when the thread stops
heart.timeout.connect(check_pulse)
# this is boiler plate, clean-up and quit
w.finished.connect(w.deleteLater)
w.finished.connect(t.quit)
app.aboutToQuit.connect(t.wait)
# move worker to thread
w.moveToThread(t)
# prevent premature exit, the heart beats after every event!
heart.start(0)
# start worker thread (event loop), and the worker
t.start()
w.start.emit()
# main application thread (event loop)
logger.info('starting application')
sys.exit(app.exec_())