難道有人請澄清爲什麼這個線程代碼調用類的方法永遠不會完成?在python線程隊列中調用類方法
from Queue import Queue
from threading import Thread
class SimpleThing(object):
def __init__(self, name):
self.name = name
def print_name(self):
print self.name
class ThingSpawner(object):
def __init__(self, name_list):
self.things = [SimpleThing(name) for name in name_list]
self.thread_queue = Queue()
def run(self):
for thing in self.things:
t = Thread(target=thing.print_name, name=thing.name)
t.daemon = True
t.start()
self.thread_queue.put(t)
self.thread_queue.join()
thing_list = ['cat', 'dog', 'llama', 'bat']
sp = ThingSpawner(thing_list)
sp.run()
代碼將明確運行print_name
方法,但不join()
並退出。
此外,什麼是最好的方式來修改此代碼,以便join()
完成?動機是使用現有的python控件類來實現一些硬件,並允許您並行調用控件類的(非常慢)方法。謝謝!