无法将线程附加到数组

时间:2016-12-09 00:58:21

标签: python arrays multithreading

所以我想将我的线程保存在一个数组中,如下所示:

threads = []

要向此数组添加线程,我创建它们并附加它们:

t = thread.start_new_thread(process_client, (client, address))
threads.append(t)

当我尝试加入时会出现问题:

for thread in threads:
    thread.join

出现以下错误:

'int' object has no attribute 'join'

我知道这里的问题是,当我创建线程时,变量t将获得一个整数,该整数将附加到数组,将其类型设置为整数。当我尝试在整数中应用方法join()时,我得到一个错误。你们中的任何人都有解决这个铸造问题的方法吗?

我在python 2.7 btw

1 个答案:

答案 0 :(得分:3)

thread.start_new_thread返回线程标识符,而不是脚本本身。所以你实际上是附加了整数的胎面标识符。

你可以创建并启动一个Thread对象(为此你需要从线程中导入Thread)然后你会有一个对它的引用

例如:

t = Thread(target=your_target, args=your_args)
t.start()
threads.append(t)