=========================================================

环境: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界面显示的更多相关文章

  1. Swing做的非阻塞式仿飞秋聊天程序

    采用Swing 布局 NIO非阻塞式仿飞秋聊天程序, 切换皮肤颜色什么的小功能以后慢慢做 启动主程序. 当用户打开主程序后自动获取局域网段IP可以在 设置 --> IP网段过滤, 拥有 JMF ...

  2. Java锁与非阻塞算法的性能比较与分析+原子变量类的应用

    15.原子变量与非阻塞同步机制 在java.util.concurrent包中的许多类,比如Semaphore和ConcurrentLinkedQueue,都提供了比使用Synchronized更好的 ...

  3. 把酒言欢话聊天,基于Vue3.0+Tornado6.1+Redis发布订阅(pubsub)模式打造异步非阻塞(aioredis)实时(websocket)通信聊天系统

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_202 "表达欲"是人类成长史上的强大"源动力",恩格斯早就直截了当地指出,处在蒙昧时代即低 ...

  4. JAVA基础知识之网络编程——-基于NIO的非阻塞Socket通信

    阻塞IO与非阻塞IO 通常情况下的Socket都是阻塞式的, 程序的输入输出都会让当前线程进入阻塞状态, 因此服务器需要为每一个客户端都创建一个线程. 从JAVA1.4开始引入了NIO API, NI ...

  5. 基于MFC的socket编程(异步非阻塞通信)

       对于许多初学者来说,网络通信程序的开发,普遍的一个现象就是觉得难以入手.许多概念,诸如:同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)等,初学者往往迷惑不清, ...

  6. Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发WebFlux 支持两种编程风(姿)格(势) 使用@Controller这种基于注解

    概述 什么是 Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发框架. 要深入了解 Spring WebFlux, 首先要了知道 R ...

  7. Socket-IO 系列(三)基于 NIO 的同步非阻塞式编程

    Socket-IO 系列(三)基于 NIO 的同步非阻塞式编程 缓冲区(Buffer) 用于存储数据 通道(Channel) 用于传输数据 多路复用器(Selector) 用于轮询 Channel 状 ...

  8. 基于CAS操作的非阻塞算法

    非阻塞算法(non-blocking algorithms)定义        所谓非阻塞算法是相对于锁机制而言的,是指:一个线程的失败或挂起不应该引起另一个线程的失败或挂起的一种算法.一般是利用硬件 ...

  9. Java并发包源码学习系列:基于CAS非阻塞并发队列ConcurrentLinkedQueue源码解析

    目录 非阻塞并发队列ConcurrentLinkedQueue概述 结构组成 基本不变式 head的不变式与可变式 tail的不变式与可变式 offer操作 源码解析 图解offer操作 JDK1.6 ...

随机推荐

  1. 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基

    题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...

  2. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. HDU——2609How many(字符串的最小表示法+substr)

    How many Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. bzoj 4804 欧拉心算 欧拉函数,莫比乌斯

    欧拉心算 Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 408  Solved: 244[Submit][Status][Discuss] Descr ...

  5. 【POJ3498】March of the Penguins(最大流,裂点)

    题意:在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃,但是它们的跳跃距离,有一个上限.  ...

  6. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. Object,String,StringBuffer,StringBuilder,System,Runtime,Date,Math介绍及用法(API)

    1       Object对象 面向对象的核心思想:“找合适的对象,做适合的事情”. 合适的对象: 自己描述类,自己创建对象. sun已经描述了好多常用的类,可以使用这些类创建对象. API(App ...

  8. 标准C程序设计七---60

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  9. 《Programming in C》读书笔记

    该书由美国Seephen G.Kochan著 贾洪峰译,电子工业出版社,来源是九江学院图书馆采购,现在藏于九江学院图书馆逸夫楼. 本书的主要内容: 第一章.基础知识 第二章.编译和运行第一个程序 第三 ...

  10. wpf GridControl selectAll UnSelect

    <Window x:Class="WpfApplication1.GridControlView" xmlns="http://schemas.microsoft. ...