基于multiprocessing和threading实现非阻塞的GUI界面显示
环境:python2.7、pyqt4、eric16.11
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'F:\workspace\pyqt5\gui_thread_pipe_subprocess\qt_main.ui'## Created by: PyQt4 UI code generator 4.11.4## WARNING! All changes made in this file will be lost!from PyQt4 import QtCore, QtGuitry:_fromUtf8 = QtCore.QString.fromUtf8except AttributeError:def _fromUtf8(s):return stry:_encoding = QtGui.QApplication.UnicodeUTF8def _translate(context, text, disambig):return QtGui.QApplication.translate(context, text, disambig, _encoding)except AttributeError:def _translate(context, text, disambig):return QtGui.QApplication.translate(context, text, disambig)class Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName(_fromUtf8("MainWindow"))MainWindow.resize(800, 600)self.centralwidget = QtGui.QWidget(MainWindow)self.centralwidget.setObjectName(_fromUtf8("centralwidget"))self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))self.textEdit = QtGui.QTextEdit(self.centralwidget)self.textEdit.setObjectName(_fromUtf8("textEdit"))self.verticalLayout.addWidget(self.textEdit)self.horizontalLayout = QtGui.QHBoxLayout()self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)self.horizontalLayout.addItem(spacerItem)self.pushButton = QtGui.QPushButton(self.centralwidget)self.pushButton.setObjectName(_fromUtf8("pushButton"))self.horizontalLayout.addWidget(self.pushButton)spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)self.horizontalLayout.addItem(spacerItem1)self.verticalLayout.addLayout(self.horizontalLayout)MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtGui.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))self.menubar.setObjectName(_fromUtf8("menubar"))MainWindow.setMenuBar(self.menubar)self.statusbar = QtGui.QStatusBar(MainWindow)self.statusbar.setObjectName(_fromUtf8("statusbar"))MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))self.pushButton.setText(_translate("MainWindow", "star", None))if __name__ == "__main__":import sysapp = QtGui.QApplication(sys.argv)MainWindow = QtGui.QMainWindow()ui = Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_())
#-*- coding: utf-8 -*-#mainWindow.pyfrom PyQt4 import QtCore, QtGuifrom Ui_qt_main import Ui_MainWindowfrom handleSubprocess import HandleSubProcessimport multiprocessing, threadingclass MainWindow(QtGui.QMainWindow, Ui_MainWindow):def __init__(self):super(MainWindow, self).__init__()self.setupUi(self)#联动按键self.btnStar = "star"self.btnStop = "stop"self.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.buttonOn)passdef buttonOn(self):if self.btnStar == self.pushButton.text():self.pushButton.setText(self.btnStop)self.starRun()passelif self.btnStop == self.pushButton.text():self.stopRun()self.pushButton.setText(self.btnStar)passpassdef stopRun(self):print "stopRun"if self.p.is_alive():self.p.terminate()self.t1.join(1)passpassdef starRun(self):#print "starRun"parent_conn, child_conn = multiprocessing.Pipe()#子进程self.p = HandleSubProcess(child_conn)self.t1 = threading.Thread(target=self.run_thread, args=(parent_conn,self.p))self.p.start()self.t1.start()passdef run_thread(self, parent_conn, pp):while pp.is_alive:self.textEdit.append(parent_conn.recv())self.delay()passprint "==== run_thread end ==================\n"passdef delay(self, timeout=9999999):cnt = timeoutwhile cnt>0:cnt -= 1passpasspass
#-*- coding: utf-8 -*-#handleSubprocess.pyimport multiprocessingclass HandleSubProcess(multiprocessing.Process):def __init__(self, child_conn):super(HandleSubProcess, self).__init__()self.child_conn = child_connpassdef run(self):cnt = 0while True:self.child_conn.send("handleSubprocess\t"+str(cnt))cnt += 1self.delay()passpassdef delay(self, timeout=9999999):cnt = timeoutwhile cnt>0:cnt -= 1passpasspass
#-*- coding: utf-8 -*-#enterPoint.pyimport sysfrom PyQt4 import QtCore, QtGuifrom mainWindow import MainWindowif __name__ == "__main__":app = QtGui.QApplication(sys.argv)ui = MainWindow()ui.show()sys.exit(app.exec_())
基于multiprocessing和threading实现非阻塞的GUI界面显示的更多相关文章
- Swing做的非阻塞式仿飞秋聊天程序
采用Swing 布局 NIO非阻塞式仿飞秋聊天程序, 切换皮肤颜色什么的小功能以后慢慢做 启动主程序. 当用户打开主程序后自动获取局域网段IP可以在 设置 --> IP网段过滤, 拥有 JMF ...
- Java锁与非阻塞算法的性能比较与分析+原子变量类的应用
15.原子变量与非阻塞同步机制 在java.util.concurrent包中的许多类,比如Semaphore和ConcurrentLinkedQueue,都提供了比使用Synchronized更好的 ...
- 把酒言欢话聊天,基于Vue3.0+Tornado6.1+Redis发布订阅(pubsub)模式打造异步非阻塞(aioredis)实时(websocket)通信聊天系统
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_202 "表达欲"是人类成长史上的强大"源动力",恩格斯早就直截了当地指出,处在蒙昧时代即低 ...
- JAVA基础知识之网络编程——-基于NIO的非阻塞Socket通信
阻塞IO与非阻塞IO 通常情况下的Socket都是阻塞式的, 程序的输入输出都会让当前线程进入阻塞状态, 因此服务器需要为每一个客户端都创建一个线程. 从JAVA1.4开始引入了NIO API, NI ...
- 基于MFC的socket编程(异步非阻塞通信)
对于许多初学者来说,网络通信程序的开发,普遍的一个现象就是觉得难以入手.许多概念,诸如:同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)等,初学者往往迷惑不清, ...
- Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发WebFlux 支持两种编程风(姿)格(势) 使用@Controller这种基于注解
概述 什么是 Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发框架. 要深入了解 Spring WebFlux, 首先要了知道 R ...
- Socket-IO 系列(三)基于 NIO 的同步非阻塞式编程
Socket-IO 系列(三)基于 NIO 的同步非阻塞式编程 缓冲区(Buffer) 用于存储数据 通道(Channel) 用于传输数据 多路复用器(Selector) 用于轮询 Channel 状 ...
- 基于CAS操作的非阻塞算法
非阻塞算法(non-blocking algorithms)定义 所谓非阻塞算法是相对于锁机制而言的,是指:一个线程的失败或挂起不应该引起另一个线程的失败或挂起的一种算法.一般是利用硬件 ...
- Java并发包源码学习系列:基于CAS非阻塞并发队列ConcurrentLinkedQueue源码解析
目录 非阻塞并发队列ConcurrentLinkedQueue概述 结构组成 基本不变式 head的不变式与可变式 tail的不变式与可变式 offer操作 源码解析 图解offer操作 JDK1.6 ...
随机推荐
- jenkins发送html测试报告
jenkins发送html测试报告 https://blog.csdn.net/galen2016/article/details/77975965/ <!DOCTYPE html> & ...
- SharePoint 2013 SSO-Secure Store Service在实际案例中的应用
文章目录: Secure Store Service介绍 Secure Store Service部署 Secure Store Service应用 之前有一篇博客讲到使用EMSManagedAPI操 ...
- Linux 之 CentOS练习
CentOS练习 参考教程:[千峰教育] 一.安装配置CentOS 1.安装虚拟机:VirtualBox. (1)软件下载:https://www.virtualbox.org/. (2)一路点击下一 ...
- php接口开发时,数据解析失败问题,字符转义,编码问题
php接口开发时,数据解析失败问题,字符转义,编码问题 情景: A平台--->向接口请求数据---->接口向B平台请求数据---->B平台返回数据给接口---->接口返回数据给 ...
- 洛谷—— P3865 【模板】ST表
https://www.luogu.org/problemnew/show/P3865 题目背景 这是一道ST表经典题——静态区间最大值 请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每 ...
- luogu P1310 表达式的值
题目描述 对于1 位二进制变量定义两种运算: 运算的优先级是: 先计算括号内的,再计算括号外的. “× ”运算优先于“⊕”运算,即计算表达式时,先计算× 运算,再计算⊕运算.例如:计算表达式A⊕B × ...
- Uncaught TypeError: window.showModalDialog is not a function
if(window.showModalDialog == undefined){ window.showModalDialog = function(url,mixedVar,features){ w ...
- spring springmvc js websocket 监听
第一步:web.xml中支持异步.所有的filter及servlet <filter> <filter-name>characterEncoding</filter-na ...
- REBXOR
题面 Description 给定一个含N个元素的数组A,下标从1开始.请找出下面式子的最大值. (A[l1]xorA[l2+1]xor-xorA[r1])+(A[l2]xorA[l2+1]xor-x ...
- 359. Logger Rate Limiter
/* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...