admin管理员组

文章数量:1599541

我正在开发一个带有一些线程的应用程序,每个线程运行一个带有时间睡眠的无限循环.我想要的是在主要完成后完成所有线程,这里是一个例子:

def main():

display_res_stop = threading.Condition()

display_result_t = threading.Thread(target=sample_t, args=(display_res_stop, ))

display_result_t.start()

time.sleep(4)

display_res_stop.acquire()

display_res_stop.notify()

display_res_stop.release()

def sample_t(stop_cond):

stop_cond.acquire()

while True:

print 5

c = stop_cond.wait(10)

stop_cond.release()

if __name__ == '__main__':

main()

这个解决方案的问题是我不知道condition.wait是否已经完成,因为超时或因为已经通知.在第二种情况下,while循环应该完成.

起初我正在做一个time.sleep(t)并使用线程事件但是应用程序必须等到所有线程都经过.

我正在考虑使用threading.Condition和Event的混合解决方案,但我不知道它是否是最好的事情(条件为’sleep’和Event替换为True).

本文标签: 已被线程通知waitpythonPython