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

环境: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. kb-07线段树-03--区间修改查询--lazy思想

    /* 区间修改,区间查询和: 第一次使用lazy思想: poj3468 */ #include<iostream> #include<cstdio> #include<c ...

  2. 爬虫【自动登陆github和抽屉】

    自动登陆github用户详情页 代码 #! /usr/bin/env python # -*- coding: utf- -*- # __author__ = "wuxiaoyu" ...

  3. 写论文,关于word的技巧

    当段落的行间距为固定值的时候,图片会出现显示不全的情况,将行间距先改了再插入图片就没问题了. 从引用那边自动添加目录. ctrl+H打开可以使用通配符,改变字母或者数字的样式等

  4. 在idea中部署远程Tomcat

    实现效果:在idea中点击run时,自动将代码编译并上传.部署到远程服务器中 和传统的在本地服务器相比较的优势:1.节省开发者开发机的资源,省去了本地服务器的CPU.内存的占用.2.如果开发的程序为A ...

  5. poj 2492 A Bug's Life 二分图染色 || 种类并查集

    题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...

  6. 15深入理解C指针之---内存释放

    一.手动申请的内存,必须及时进行内存释放,否则容易造成内存泄露.主要代码形式为: #include <stdio.h> #include <stdlib.h> int main ...

  7. 反汇编->C++虚函数深度分析

    先来查看一简单例子 #include<iostream> using namespace std; class Base{ public: virtual void f() { cout ...

  8. 不要使用 reader.Peek() 去读取每行数据

    1.问题描述 使用SteamRead的Peek()和ReadLine()来读取流中的数据,如果数据行数太多,会读取不完整(后面有些数据就读不出来了). 比如: while (srResponseRea ...

  9. Codeforces 889C Maximum Element(DP + 计数)

    题目链接  Maximum Element 题意  现在有这一段求序列中最大值的程度片段: (假定序列是一个1-n的排列) int fast_max(int n, int a[]) { int ans ...

  10. 新华三孟丹:NFV资源池实现中的技术探讨

    近日,在第三届未来网络发展大会SDN/NFV技术与应用创新分论坛上,新华三解决方案部架构师孟丹女士发表了主题为<NFV资源池实现中的技术探讨>的主题演讲. 孟丹指出,新华三的NFV核心理念 ...