2011-11-24 157 views
1
thread.start_new_thread(target=self.socketFunctionRead1()) 
print "Thread 1" 
thread.start_new_thread(target=self.socketFunctionWrite1()) 
print "Thread 2" 
thread.start_new_thread(target=self.socketFunctionRead2()) 
print "Thread 3" 
thread.start_new_thread(target=self.socketFunctionWrite2()) 
print "Thread 4" 

我想启动多个线程,但只有一个线程启动,我怎样才能让程序进一步通过启动其他线程?Python线程:只有一个线程启动

回答

2

相反的thread.start_new_thread(target=self.socketFunctionRead1())

尝试由于parenthese的thread.start_new_thread(target=self.socketFunctionRead1)

,调用该函数和的返回值功能被分配给目标。由于thread.start_new_thread(target=self.socketFunctionRead1())可能是一个阻塞调用,只有这个函数被调用。

在thread.start_new_thread中,目标应该是可调用的(一个对象的行为像一个函数)。

编辑:

Python documentation

thread.start_new_thread(函数,ARGS [,kwargs])

开始一个新的线程 和返回它的识别符。该线程使用参数列表args(它必须是一个元组)执行功能函数 。可选的 kwargs参数指定关键字参数的字典。当 函数返回时,该线程静静地退出。当函数 以未处理的异常终止时,将打印堆栈跟踪,然后线程退出(但其他线程继续运行) 。

这意味着你应该调用thread.start_new_thread(self.socketFunctionRead1)。

如果您将关键字参数传递给start_new_thread,它们将传递给self.socketFunctionRead1。

您的线程的目标是必需的,而不是关键字参数。

+1

现在我得到了:start_new_thread()不带任何关键字参数:/ –

0

也许你只是从来没有等待线程结束前退出程序...

当程序结束时,退出,并杀死所有线程。您必须等待所有线程结束前退出你的程序,就像埃米尔Akaydın说