同时运行2循环python

时间:2016-07-07 19:17:13

标签: python multithreading segmentation-fault pyqt

foo():
   while True: 
       #do smthng

boo():
   while True:
       #do smthng

每当按下按钮时,我都会使用这两种方法:

Thread(target=foo).start()
Thread(target=boo).start()

在我的代码中,foo是按钮并用于填充进度条。 boo是按钮,用于撤消填充进度条。

当我按foo触发按钮时没有问题显示我按下boo触发按钮如果变空我再按foo我得到分割错误。

当按下boo时如何停止foo线程?当foo按下时如何停止boo线程?

编辑: 我的所有代码都是:

import time
import sys
from PyQt4 import QtGui, QtCore
import threading

QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)


class CustomThread(threading.Thread):
    def __init__(self, target=None):
        super(CustomThread, self).__init__(target=target)
        self._stop = threading.Event()

    def stop(self):
        self._stop.set()




class CustomProgressBar(QtGui.QProgressBar):
    def __init__(self, x, y, lenght, height, require_list=[]):
        QtGui.QProgressBar.__init__(self)
        self.require_list = require_list
        self.setGeometry(x, y, lenght, height)
        self.speed = 0

    def fill(self):
        while self.speed > 0 and self.value() < 100:
            self.setValue(self.value()+self.speed)
            time.sleep(0.01)
        while self.speed < 0 and self.value() > 0:
            self.setValue(self.value()+self.speed)
            time.sleep(0.01)
        self.speed = 0


class Valve(QtGui.QCheckBox):
    def __init__(self, x, y, lenght, height, inputs=[], outputs=[]):
        super(Valve, self).__init__()
        self.sources = inputs
        self.outputs = outputs
        self.setGeometry(x, y, lenght, height)
        self.stateChanged.connect(self.filler)
        self.fill_thread_list = []

    def is_fillable(self):
        for source in self.sources:
            if source.value() == 100: 
                return 1
        return 0

    def filler(self):
        for thread in self.fill_thread_list:
            thread.stop()

        for output in self.outputs:
            if self.is_fillable():
                fillThread = CustomThread(target=output.fill)
                self.fill_thread_list.append(fillThread)
                if self.isChecked():
                    output.speed = 1
                else:
                    output.speed = -1
                fillThread.start()


class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.initUI()

    def initUI(self):
        layout = QtGui.QHBoxLayout()

        #pipes and buttons
        self.pipe = CustomProgressBar(20, 50, 100, 2)
        self.pipe.setValue(100)
        self.pipe2 = CustomProgressBar(120, 50, 100, 2, require_list=[self.pipe])
        self.pipe3 = CustomProgressBar(120, 50, 100, 2, require_list=[self.pipe])
        self.button1 = Valve(100, 50, 10, 10, inputs=[self.pipe], outputs=[self.pipe2, self.pipe3])


        #add buttons to layout
        layout.addWidget(self.button1)
        layout.addWidget(self.pipe)
        layout.addWidget(self.pipe2)
        layout.addWidget(self.pipe3)
        self.setLayout(layout)

        self.setGeometry(0, 0, 1366, 768)
        self.show()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

它与此question不同。

0 个答案:

没有答案