我需要写一个线程:
运行n秒,然后执行 A(并退出)
除非它的父级告诉它提前停止,在这种情况下它会执行 B(并退出)
我写了一些有效但非常复杂的东西(我相信)。它(错误)使用 aLock()
作为标志:
import threading
import time
def countdown(lock):
t = 0
while t < 10:
t += 1
print(t)
try:
lock.release()
except RuntimeError:
# was not told to die (= lock is not set)
pass
else:
# the parent notified to kill by setting the lock
print("child: the lock has been set, means I die before timeout")
return
time.sleep(1)
# executed when timeouting
print("child: I timeouted on my own")
return
lock = threading.Lock()
# with timeout
threading.Thread(target=countdown, args=(lock,)).start()
print("parent: sleeping 12 seconds, the thread should timeout in the meantime")
time.sleep(12)
lock = threading.Lock()
# without timeout
threading.Thread(target=countdown, args=(lock,)).start()
print("parent: sleeping 5 seconds, and then setting the flag")
time.sleep(5)
lock.acquire()
print("the counter should exit prematurely while I continue sleeping")
time.sleep(5)
它工作正常(我不关心时间的细微变化 - 在这种情况下是 6 秒 vs 5 秒,因为主线程和生成的线程共同运行):
1
parent: sleeping 12 seconds, the thread should timeout in the meantime
2
3
4
5
6
7
8
9
10
child: I timeouted on my own
1
parent: sleeping 5 seconds, and then setting the flag
2
3
4
5
the counter should exit prematurely while I continue sleeping
6
child: the lock has been set, means I die before timeout
正如我所提到的,这个解决方案对我来说看起来非常复杂。
是否有更 Pythonic 的构造可以运行超时线程,可被其父线程中断?
皈依舞
相关分类