【发布时间】:2016-01-13 17:57:50
【问题描述】:
我在脚本中使用模块APScheduler,我使用BlockingScheduler。我有一些周期性的工作。如果这项工作raise 和Exception,无论我在expect 它在try 中还是让它传播,我的线程都不会返回。然后我到达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__() 方法,即使是 __enter__() 引发异常也是如此。在睾丸之后,我看到__exit__() 没有被调用就是这样的场景。因此会导致死锁和线程卡住。
我应该如何解决这个问题?
看起来__enter__() 不能引发异常。对吗?
【问题讨论】:
标签: python python-3.x exception apscheduler