2018-10-07 07:33:33 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
from Queue import Queue
|
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
|
|
class PromiseResultType(Enum):
|
|
|
|
Result = 1
|
|
|
|
Exception = 2
|
2018-10-09 10:06:23 +00:00
|
|
|
Traceback = 3
|
2018-10-11 02:26:17 +00:00
|
|
|
|
2018-10-07 07:33:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Promise(object):
|
|
|
|
"""
|
|
|
|
用队列实现的一个简单的Promise类型
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._queue = Queue()
|
|
|
|
|
|
|
|
def set_result(self, val):
|
|
|
|
self._queue.put((PromiseResultType.Result, val))
|
|
|
|
|
|
|
|
def get(self, timeout=None):
|
|
|
|
res = self._queue.get(timeout=timeout)
|
|
|
|
if res[0] == PromiseResultType.Result:
|
|
|
|
return res[1]
|
2018-10-09 10:06:23 +00:00
|
|
|
elif res[0] == PromiseResultType.Exception:
|
|
|
|
raise res[1]
|
|
|
|
else:
|
|
|
|
et, ev, tb = res[1]
|
2018-11-01 06:01:36 +00:00
|
|
|
raise et(ev).with_traceback(tb)
|
2018-10-07 07:33:33 +00:00
|
|
|
|
2018-10-09 10:06:23 +00:00
|
|
|
def set_exception(self, valueOrType, val=None, tb=None):
|
|
|
|
if val is None:
|
2018-10-11 02:09:27 +00:00
|
|
|
self._queue.put((PromiseResultType.Exception, valueOrType))
|
2018-10-09 10:06:23 +00:00
|
|
|
else:
|
|
|
|
self._queue.put((PromiseResultType.Traceback, (valueOrType, val, tb)))
|
2018-10-11 02:26:17 +00:00
|
|
|
|
|
|
|
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)
|