我有多个线程使用事件对象等待超时。如果我想在事件上调用set(),这将解除阻塞所有线程。什么是打开特定线程并使其他线程处于等待状态的好方法?
我考虑的不是等待,而是使用一个全局变量作为条件来指示线程何时返回,而不是等待,但是我不知道这如何保持我想要的每个线程的超时,而不检查时间戳。
import threading
import time
t1 = threading.Thread(target=startTimeout)
t2 = threading.Thread(target=startTimeout)
timeoutEvent = threading.Event()
time.sleep(0.3)
timeoutEvent.set()
# How to have indivial timeoutEvents for specific threads?
def startTimeout():
check = timeoutEvent.wait(1)
if (check):
# set was called
else:
# Timeout发布于 2022-11-11 17:11:42
您可以为每个线程创建一个事件,或者一组线程,只需将其作为参数传递给它们或存储在某个地方即可。
import threading
import time
def startTimeout(event):
check = event.wait(1)
if (check):
print('pass')
else:
print("didn't pass")
timeoutEvent1 = threading.Event()
timeoutEvent2 = threading.Event()
t1 = threading.Thread(target=startTimeout,args=(timeoutEvent1,))
t2 = threading.Thread(target=startTimeout,args=(timeoutEvent2,))
t1.start()
t2.start()
time.sleep(0.3)
timeoutEvent1.set()
t1.join()
t2.join()pass
didn't passhttps://stackoverflow.com/questions/74405721
复制相似问题