我需要写一个线程:运行n秒,然后执行 A(并退出)除非它的父级告诉它提前停止,在这种情况下它会执行 B(并退出)我写了一些有效但非常复杂的东西(我相信)。它(错误)使用 aLock()作为标志:import threadingimport timedef 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") returnlock = threading.Lock()# with timeoutthreading.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 timeoutthreading.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 秒,因为主线程和生成的线程共同运行):1parent: sleeping 12 seconds, the thread should timeout in the meantime2345678910child: I timeouted on my own1parent: sleeping 5 seconds, and then setting the flag2345the counter should exit prematurely while I continue sleeping6child: the lock has been set, means I die before timeout正如我所提到的,这个解决方案对我来说看起来非常复杂。是否有更 Pythonic 的构造可以运行超时线程,可被其父线程中断?
1 回答
皈依舞
TA贡献1851条经验 获得超3个赞
如果额外的线程仅用于倒计时,同时不执行任何其他功能,则该threading.Timer()对象就是为此而创建的。
如果需要,可以使用该.cancel()方法提前取消计时器。更多信息可以在官方文档中找到。
从问题中重写示例(稍作修改)使代码看起来像:
import threading
import time
# with timeout
t = threading.Timer(10.0, lambda: print("I timed out"))
t.start()
print("Parent: Sleeping 12 seconds, the thread should timeout in the meantime")
time.sleep(12)
print("Done sleeping")
# without timeout
t = threading.Timer(10.0, lambda: print("I timed out"))
t.start()
print("Parent: Sleeping 6 seconds, and then cancel timers")
time.sleep(6)
t.cancel()
print("The counter is canceled while I continue sleeping")
time.sleep(6)
print("Done sleeping")
添加回答
举报
0/150
提交
取消