Python线程:只启动一个线程

2024-10-01 15:34:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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"

我试图启动多个线程,但只有一个线程被启动,如何通过启动其他线程使程序更进一步?在


Tags: self程序targetnew线程threadstartprint
3条回答

而不是thread.start_new_thread(target=self.socketFunctionRead1())

尝试thread.start_new_thread(target=self.socketFunctionRead1)

因为有圆括号,函数被调用,函数的返回值被分配给target。由于thread.start_new_thread(target=self.socketFunctionRead1())可能是一个阻塞调用,因此只调用此函数。在

在thread.start_new_线程,目标应该是可调用的(一个行为类似于函数的对象)。在

编辑:

Python documentation

thread.start_new_thread(function, args[, kwargs])

Start a new thread and return its identifier. The thread executes the function function with the argument list args (which must be a tuple). The optional kwargs argument specifies a dictionary of keyword arguments. When the function returns, the thread silently exits. When the function terminates with an unhandled exception, a stack trace is printed and then the thread exits (but other threads continue to run).

这意味着你应该打电话thread.start_new_线程(self.socketFunctionRead1). 在

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

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

也许你永远不会等到线程结束后才退出程序。。。在

当程序结束时,它退出,并杀死所有线程。你必须等待所有线程结束后才能退出程序,正如埃米尔·阿卡伊德所说

相关问题 更多 >

    热门问题