基于multiprocessing和threading实现非阻塞的GUI界面显示

Posted naedzq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于multiprocessing和threading实现非阻塞的GUI界面显示相关的知识,希望对你有一定的参考价值。

=========================================================
环境:python2.7、pyqt4、eric16.11
热点:multiprocessing、threading、GUI、pyqt
需求:
希望界面上的QTextEdit控件可以滚动刷新日志,且软件界面不可以有阻塞。
=========================================================

郑重提示:如欲转载,请注明出处。

最终实现效果如下:
 
该实现的核心思想是:
先建立一个pipe,write端传给子进程,子进程无限发数据,read端传给子线程,在子进程活动的情况下无限取数据;子线程中再把read到的数据写入QTextEdit控件上。下面贴上详细的代码:
Ui_qt_main.py
  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file \'F:\\workspace\\pyqt5\\gui_thread_pipe_subprocess\\qt_main.ui\'
  3. #
  4. # Created by: PyQt4 UI code generator 4.11.4
  5. #
  6. # WARNING! All changes made in this file will be lost!
  7. from PyQt4 import QtCore, QtGui
  8. try:
  9. _fromUtf8 = QtCore.QString.fromUtf8
  10. except AttributeError:
  11. def _fromUtf8(s):
  12. return s
  13. try:
  14. _encoding = QtGui.QApplication.UnicodeUTF8
  15. def _translate(context, text, disambig):
  16. return QtGui.QApplication.translate(context, text, disambig, _encoding)
  17. except AttributeError:
  18. def _translate(context, text, disambig):
  19. return QtGui.QApplication.translate(context, text, disambig)
  20. class Ui_MainWindow(object):
  21. def setupUi(self, MainWindow):
  22. MainWindow.setObjectName(_fromUtf8("MainWindow"))
  23. MainWindow.resize(800, 600)
  24. self.centralwidget = QtGui.QWidget(MainWindow)
  25. self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
  26. self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
  27. self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
  28. self.textEdit = QtGui.QTextEdit(self.centralwidget)
  29. self.textEdit.setObjectName(_fromUtf8("textEdit"))
  30. self.verticalLayout.addWidget(self.textEdit)
  31. self.horizontalLayout = QtGui.QHBoxLayout()
  32. self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
  33. spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
  34. self.horizontalLayout.addItem(spacerItem)
  35. self.pushButton = QtGui.QPushButton(self.centralwidget)
  36. self.pushButton.setObjectName(_fromUtf8("pushButton"))
  37. self.horizontalLayout.addWidget(self.pushButton)
  38. spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
  39. self.horizontalLayout.addItem(spacerItem1)
  40. self.verticalLayout.addLayout(self.horizontalLayout)
  41. MainWindow.setCentralWidget(self.centralwidget)
  42. self.menubar = QtGui.QMenuBar(MainWindow)
  43. self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
  44. self.menubar.setObjectName(_fromUtf8("menubar"))
  45. MainWindow.setMenuBar(self.menubar)
  46. self.statusbar = QtGui.QStatusBar(MainWindow)
  47. self.statusbar.setObjectName(_fromUtf8("statusbar"))
  48. MainWindow.setStatusBar(self.statusbar)
  49. self.retranslateUi(MainWindow)
  50. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  51. def retranslateUi(self, MainWindow):
  52. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
  53. self.pushButton.setText(_translate("MainWindow", "star", None))
  54. if __name__ == "__main__":
  55. import sys
  56. app = QtGui.QApplication(sys.argv)
  57. MainWindow = QtGui.QMainWindow()
  58. ui = Ui_MainWindow()
  59. ui.setupUi(MainWindow)
  60. MainWindow.show()
  61. sys.exit(app.exec_())
mainWindow.py
  1. #-*- coding: utf-8 -*-
  2. #mainWindow.py
  3. from PyQt4 import QtCore, QtGui
  4. from Ui_qt_main import Ui_MainWindow
  5. from handleSubprocess import HandleSubProcess
  6. import multiprocessing, threading
  7. class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
  8. def __init__(self):
  9. super(MainWindow, self).__init__()
  10. self.setupUi(self)
  11. #联动按键
  12. self.btnStar = "star"
  13. self.btnStop = "stop"
  14. self.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.buttonOn)
  15. pass
  16. def buttonOn(self):
  17. if self.btnStar == self.pushButton.text():
  18. self.pushButton.setText(self.btnStop)
  19. self.starRun()
  20. pass
  21. elif self.btnStop == self.pushButton.text():
  22. self.stopRun()
  23. self.pushButton.setText(self.btnStar)
  24. pass
  25. pass
  26. def stopRun(self):
  27. print "stopRun"
  28. if self.p.is_alive():
  29. self.p.terminate()
  30. self.t1.join(1)
  31. pass
  32. pass
  33. def starRun(self):
  34. #print "starRun"
  35. parent_conn, child_conn = multiprocessing.Pipe()
  36. #子进程
  37. self.p = HandleSubProcess(child_conn)
  38. self.t1 = threading.Thread(target=self.run_thread, args=(parent_conn,self.p))
  39. self.p.start()
  40. self.t1.start()
  41. pass
  42. def run_thread(self, parent_conn, pp):
  43. while pp.is_alive:
  44. self.textEdit.append(parent_conn.recv())
  45. self.delay()
  46. pass
  47. print "==== run_thread end ==================\\n"
  48. pass
  49. def delay(self, timeout=9999999):
  50. cnt = timeout
  51. while cnt>0:
  52. cnt -= 1
  53. pass
  54. pass
  55. pass
handleSubprocess.py
  1. #-*- coding: utf-8 -*-
  2. #handleSubprocess.py
  3. import multiprocessing
  4. class HandleSubProcess(multiprocessing.Process):
  5. def __init__(self, child_conn):
  6. super(HandleSubProcess, self).__init__()
  7. self.child_conn = child_conn
  8. pass
  9. def run(self):
  10. cnt = 0
  11. while True:
  12. self.child_conn.send("handleSubprocess\\t"+str(cnt))
  13. cnt += 1
  14. self.delay()
  15. pass
  16. pass
  17. def delay(self, timeout=9999999):
  18. cnt = timeout
  19. while cnt>0:
  20. cnt -= 1
  21. pass
  22. pass
  23. pass
enterPoint.py
  1. #-*- coding: utf-8 -*-
  2. #enterPoint.py
  3. import sys
  4. from PyQt4 import QtCore, QtGui
  5. from mainWindow import MainWindow
  6. if __name__ == "__main__":
  7. app = QtGui.QApplication(sys.argv)
  8. ui = MainWindow()
  9. ui.show()
  10. sys.exit(app.exec_())
以此与同道中人共勉。





以上是关于基于multiprocessing和threading实现非阻塞的GUI界面显示的主要内容,如果未能解决你的问题,请参考以下文章

Python学习笔记18:标准库之多进程(multiprocessing包)

基于multiprocessing和threading实现非阻塞的GUI界面显示

基于multiprocessing.dummy线程池爬取梨视频的视频信息

multiprocessing库:Python像线程一样管理进程

multiprocessing库:Python像线程一样管理进程

网络编程——进程同步