0

My goal for a task is to allow one button press to start two processes, both running simultaneously on different QThreads.

My code is structured like this (simplified)

class Main_Window():

    # My UI stuff goes here

class worker1(QtCore.QObject):
    def __init__(self):
        ...

    def run1():
        ...

class worker2(QtCore.QObject):
    def __init__(self):
        ...

    def run2():
        ...

app = QtGui.QApplication(sys.argv)
myapp = Main_Window()

thr1 = QtCore.QThread()
thr2 = QtCore.QThread()

work1 = worker1()
work2 = worker2()

work1.moveToThread(thr1)
work2.moveToThread(thr2)

# I have a signal coming in from main thread
app.connect(myapp, QtCore.SIGNAL('start1'), work1.run1())
app.connect(myapp, QtCore.SIGNAL('start1'), work2.run2())

thr1.start()
thr2.start()

Is this kind of QThread coding incorrect if I want to setup two Qthreads?

I am getting a "Segmentation fault" when I try to start the program, but as soon as I take the second app.connect away, it runs fine.

I was wondering if anyone can tell me where I've gone wrong.

Thanks!

jgv115
  • 499
  • 5
  • 17
  • It looks fine. But I think problem is inside your `run1()` and `run2()` methods. They may use the same resources and you get crash. What are they for? – Max Pashkov Apr 28 '17 at 11:43
  • run1() has been there for a long time and it has been working fine. I only recently tried to add run2(). The run2() method only has a loop that increments a counter and prints it, it doesn't actually do anything at the moment. What do you mean they might use the same resources? :/ – jgv115 Apr 30 '17 at 10:29

1 Answers1

0

When you connect your signals with:

app.connect(myapp, QtCore.SIGNAL('start1'), work1.run1())

You are actually executing the run function, not just connecting it. You want to leave out the "()" or else python executes the function and tries to connect whatever it returns.

EDIT:

Two more suggestions in response to your comment saying you took out the "()". First, I've never seen someone rename the run function when using the QtThread class and you may want to try the same code where both run1 and run2 are actually just named "run". Check out this thread for some good example: How to use QThread correctly in pyqt with moveToThread()?

Second, can you post the actual error? Does it like anything like the one in this thread: Is this PyQt 4 python bug or wrongly behaving code?

Community
  • 1
  • 1
aoh
  • 1,090
  • 2
  • 13
  • 25
  • Thanks for the response. What you are saying makes sense. I actually don't have the () in there in my code, still pops up with segmentation fault. Check my comment above for what's actually in the run methods – jgv115 Apr 30 '17 at 10:25