[英]Can't append thread to array
So I want to save my threads in an array, like this: 所以我想将我的线程保存在一个数组中,如下所示:
threads = []
To add threads to this array, I create them and append them: 要向此数组添加线程,我创建它们并附加它们:
t = thread.start_new_thread(process_client, (client, address))
threads.append(t)
The problem happens when I try to join them: 当我尝试加入它们时会发生问题:
for thread in threads:
thread.join
The following error appears: 出现以下错误:
'int' object has no attribute 'join'
I know the problem here is that when I create the thread the variable t
will get an integer that will be appended to the array, setting its type to integer. 我知道这里的问题是,当我创建线程时,变量t
将获得一个整数,该整数将附加到数组,将其类型设置为整数。 And when I try to apply the method join()
in an integer I get an error. 当我尝试在整数中应用join()
方法时,我得到一个错误。 Do any of you guys have a solution to this casting problem? 你们中的任何人都有解决这个铸造问题的方法吗?
I'm on python 2.7 btw 我正在使用python 2.7 btw
thread.start_new_thread
returns the thread identifier, not the tread itself. thread.start_new_thread
返回线程标识符,而不是thread.start_new_thread
本身。 So you are actually appending the tread identifiers which are integers. 所以你实际上是附加了整数的胎面标识符。
You could create and start a Thread object (for that you need to import Thread from threading) then you would have a reference to it 您可以创建并启动一个Thread对象(为此您需要从线程中导入Thread),然后您将获得对它的引用
Ex: 例如:
t = Thread(target=your_target, args=your_args)
t.start()
threads.append(t)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.