I am using PyQt4 and Python 2.7.
I have two functions to start up at the same time. But the second function doesn't start up until the first ends.
What is going wrong?
def testvision():
x=5
while x>0:
print 'vision'
time.sleep(1)
x=x-1
print 'finish vision'
def testforword():
x=5
while x>0:
print 'froword'
time.sleep(1)
x=x-1
print 'finish forword'
def Forword_thread(self):
t1 = threading.Thread(target=testvision())
t2 = threading.Thread(target=testforword())
t1.start()
t2.start()
t1.join()
t2.join()
I call the function in this way:
self.pushbuttonForword.clicked.connect(self.Forword_thread)
Your creating your threads like this:
t1 = threading.Thread(target=testvision())
Which is equivalent to:
target = testvision() # returns None, so target is None now
t1 = threading.Thread(target=target) # passes None as target
That means the function testvision()
is executed in the current thread, the new thread is created with an empty target method, which is the same as using Thread()
. When started, this thread will invoke its (empty) run()
method and exit immediately.
The correct way would be to use
t1 = threading.Thread(target=testvision)
Same for t2
.