本文整理汇总了Python中aplus.Promise类的典型用法代码示例。如果您正苦于以下问题:Python Promise类的具体用法?Python Promise怎么用?Python Promise使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Promise类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handle_server_status
def _handle_server_status(self, msg_json):
self.fee.set_load_scale(msg_json)
p = Promise()
p.fulfill(self.fee.calculate_fee())
self.fee_promise = p
return True
示例2: testNonFunction
def testNonFunction(nonFunction):
def foo(k, r):
results[k] = r
p1 = Promise()
p2 = p1.then(nonFunction, lambda r: foo(str(nonFunction), r))
p1.reject(Exception("Error: " + str(nonFunction)))
示例3: _handle_ledger_closed
def _handle_ledger_closed(self, msg_json):
self.fee.set_fee_scale(msg_json)
p = Promise()
p.fulfill(self.fee.calculate_fee())
self.fee_promise = p
return True
示例4: test_3_2_6_5_rejected
def test_3_2_6_5_rejected():
"""
Handles the case where the arguments to then
are values, not functions or promises.
"""
p1 = Promise()
p1.reject("Error")
p2 = p1.then(None, 5)
assert_equals("Error", p1.reason)
assert p2.isRejected()
assert_equals("Error", p2.reason)
示例5: test_exceptions
def test_exceptions():
def throws(v):
assert False
p1 = Promise()
p1.addCallback(throws)
p1.fulfill(5)
p2 = Promise()
p2.addErrback(throws)
p2.reject("Error")
示例6: test_3_2_6_4_fulfilled
def test_3_2_6_4_fulfilled():
"""
Handles the case where the arguments to then
are values, not functions or promises.
"""
p1 = Promise()
p1.fulfill(10)
p2 = p1.then(5)
assert_equals(10, p1.value)
assert p2.isFulfilled()
assert_equals(10, p2.value)
示例7: test_3_2_6_5_rejected
def test_3_2_6_5_rejected():
"""
Handles the case where the arguments to then
are values, not functions or promises.
"""
p1 = Promise()
p1.reject(Exception("Error"))
p2 = p1.then(None, 5)
assert_exception(p1.reason, Exception, "Error")
assert p2.isRejected
assert_exception(p2.reason, Exception, "Error")
示例8: test_3_2_6_1_literally
def test_3_2_6_1_literally():
"""
If _either_ onFulfilled or onRejected returns a value
that is not a promise, promise2 must be fulfilled with that value
"""
def _raise(e):
raise e
p1 = Promise()
pf = p1.then(lambda v : _raise(Exception("Error")), lambda x : "catched")
p1.fulfill(5)
assert_equals(pf.value, "catched")
示例9: test_3_2_3_3
def test_3_2_3_3():
"""
Make sure rejected callback never called if promise is fulfilled
"""
cf = Counter()
cr = Counter()
p1 = Promise()
p2 = p1.then(lambda v: cf.tick(), lambda r: cr.tick())
p1.fulfill(5)
assert_equals(0, cr.value())
assert_equals(1, cf.value())
示例10: test_exceptions
def test_exceptions():
def throws(v):
assert False
p1 = Promise()
p1.addCallback(throws)
p1.fulfill(5)
p2 = Promise()
p2.addErrback(throws)
p2.reject(Exception())
assert_raises(Exception, p2.get)
示例11: test_3_2_3_1
def test_3_2_3_1():
"""
The second argument to 'then' must be called when a promise is
rejected.
"""
c = Counter()
def check(r, c):
assert_equals(r, "Error")
c.tick()
p1 = Promise()
p2 = p1.then(None, lambda r: check(r, c))
p1.reject("Error")
assert_equals(1, c.value())
示例12: test_3_2_2_1
def test_3_2_2_1():
"""
The first argument to 'then' must be called when a promise is
fulfilled.
"""
c = Counter()
def check(v, c):
assert_equals(v, 5)
c.tick()
p1 = Promise()
p2 = p1.then(lambda v: check(v, c))
p1.fulfill(5)
assert_equals(1, c.value())
示例13: test_3_2_5_2_if
def test_3_2_5_2_if():
"""
Then can be called multiple times on the same promise
and callbacks must be called in the order of the
then calls.
"""
def add(l, v):
l.append(v)
p1 = Promise()
p1.reject("Error")
order = []
p2 = p1.then(None, lambda v: add(order, "p2"))
p3 = p1.then(None, lambda v: add(order, "p3"))
assert_equals(2, len(order))
assert_equals("p2", order[0])
assert_equals("p3", order[1])
示例14: test_3_2_1
def test_3_2_1():
"""
Test that the arguments to 'then' are optional.
"""
p1 = Promise()
p2 = p1.then()
p3 = Promise()
p4 = p3.then()
p1.fulfill(5)
p3.reject("How dare you!")
示例15: test_list_promise_if
def test_list_promise_if():
p1 = Promise()
p1.fulfill(5)
p2 = Promise()
p2.fulfill(10)
pl = listPromise(p1, p2)
assert p1.isFulfilled()
assert p2.isFulfilled()
assert pl.isFulfilled()
assert_equals(5, p1.value)
assert_equals(10, p2.value)
assert_equals(5, pl.value[0])
assert_equals(10, pl.value[1])