[Add] Promise增加with self.catch():语法支持
This commit is contained in:
parent
ab116d8f97
commit
3f8e5f1099
@ -10,6 +10,7 @@ class PromiseResultType(Enum):
|
|||||||
Traceback = 3
|
Traceback = 3
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Promise(object):
|
class Promise(object):
|
||||||
"""
|
"""
|
||||||
用队列实现的一个简单的Promise类型
|
用队列实现的一个简单的Promise类型
|
||||||
@ -36,3 +37,23 @@ class Promise(object):
|
|||||||
self._queue.put((PromiseResultType.Exception, valueOrType))
|
self._queue.put((PromiseResultType.Exception, valueOrType))
|
||||||
else:
|
else:
|
||||||
self._queue.put((PromiseResultType.Traceback, (valueOrType, val, tb)))
|
self._queue.put((PromiseResultType.Traceback, (valueOrType, val, tb)))
|
||||||
|
|
||||||
|
def catch(self):
|
||||||
|
"""
|
||||||
|
Usage :
|
||||||
|
with promise.catch():
|
||||||
|
raise Exception();
|
||||||
|
"""
|
||||||
|
return _PromiseCatchContext(self)
|
||||||
|
|
||||||
|
|
||||||
|
class _PromiseCatchContext(object):
|
||||||
|
|
||||||
|
def __init__(self, promise):
|
||||||
|
self.promise = promise
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
self.promise.set_exception(exc_type, exc_val, exc_tb)
|
||||||
|
Loading…
Reference in New Issue
Block a user