1
foo():
   while True: 
       #do smthng

boo():
   while True:
       #do smthng

I will use this two method whenever my button pushed as:

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

In my code foo is button and for filling progress bar. boo is button and for undo filling progress bar.

When I press foo trigger button there is no problem shows when I press boo trigger button if get empty then I press foo again I got segmentation error.

How can I stop foo thread when boo pressed and boo thread when foo pressed?

Edit: My all code is:

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()

It just different from this question.

Community
  • 1
  • 1
ihsancemil
  • 432
  • 5
  • 16
  • Possible duplicate of [Is there any way to kill a Thread in Python?](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python) – Tom Myddeltyn Jul 07 '16 at 19:26
  • @busfault I tried this but when I say firstTread.close(), secondThread.start() it just still popping segmentation fault – ihsancemil Jul 07 '16 at 20:05
  • Thank you for adding all that, it makes things different. Have you tried `QThreads`? – Tom Myddeltyn Jul 07 '16 at 22:22
  • 1
    Your program is segfaulting because you are accessing/modifying Qt objects from a thread. This is forbidden. You need to start `QThreads` imsyead of Python threads so you can emit a signal from your QThread back to the main thread where the GUI can be updated safely. – three_pineapples Jul 07 '16 at 22:44

0 Answers0