我在脚本中使用模块APScheduler,我使用BlockingScheduler。我有一些定期工作。如果这个作业raise是Exception,无论我在try中对它进行expect还是让它传播,我的线程都不会返回。然后我到达max_instance,不再执行任何作业。在
当使用BlockingScheduler时,我应该如何管理线程中的Execptions?在
我的MWE说明了我的问题:from apscheduler.schedulers.blocking import BlockingScheduler
import threading
class x:
def __init__(self):
self._lock = threading.Lock()
def __enter__(self):
print("ENTER")
self._lock.acquire()
print("LOCK")
raise Exception("ERROR")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("EXIT")
self._lock.release()
print("UNLOCK")
a = x()
def test():
print("TEST")
with a:
print("WITH")
pollingScheduler = BlockingScheduler()
pollingScheduler.add_job(test, 'interval', seconds=1, max_instances=1)
pollingScheduler.start()
我期望在引发异常时必须调用__exit__()方法,即使是{}引发异常。在测试之后,我看到__exit__()不被调用就是这样的场景。因此,它会导致死锁和线程卡住。在
我该怎么解决这个问题?
看起来__enter__()不能引发异常。对吗?在
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)