多线程不能一起工作
multithreading do not work togather
我正在使用 PyQt4 和 Python 2.7.
我有两个功能要同时启动。但是第二个功能直到第一个结束才开始。
出了什么问题?
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()
我是这样调用函数的:
self.pushbuttonForword.clicked.connect(self.Forword_thread)
你这样创建线程:
t1 = threading.Thread(target=testvision())
相当于:
target = testvision() # returns None, so target is None now
t1 = threading.Thread(target=target) # passes None as target
表示函数testvision()
在当前线程中执行,新线程是用一个空的目标方法创建的,与使用Thread()
是一样的。启动时,此线程将调用其(空)run()
方法并立即退出。
正确的方法是使用
t1 = threading.Thread(target=testvision)
与 t2
相同。
我正在使用 PyQt4 和 Python 2.7.
我有两个功能要同时启动。但是第二个功能直到第一个结束才开始。
出了什么问题?
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()
我是这样调用函数的:
self.pushbuttonForword.clicked.connect(self.Forword_thread)
你这样创建线程:
t1 = threading.Thread(target=testvision())
相当于:
target = testvision() # returns None, so target is None now
t1 = threading.Thread(target=target) # passes None as target
表示函数testvision()
在当前线程中执行,新线程是用一个空的目标方法创建的,与使用Thread()
是一样的。启动时,此线程将调用其(空)run()
方法并立即退出。
正确的方法是使用
t1 = threading.Thread(target=testvision)
与 t2
相同。