本文整理汇总了Python中hazelcast.future.Future类的典型用法代码示例。如果您正苦于以下问题:Python Future类的具体用法?Python Future怎么用?Python Future使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Future类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Invocation
class Invocation(object):
sent_connection = None
timer = None
def __init__(self, request, partition_id=-1, address=None, connection=None, timeout=INVOCATION_TIMEOUT):
self._event = threading.Event()
self.timeout = timeout + time.time()
self.address = address
self.connection = connection
self.partition_id = partition_id
self.request = request
self.future = Future()
def has_connection(self):
return self.connection is not None
def has_partition_id(self):
return self.partition_id >= 0
def has_address(self):
return self.address is not None
def set_response(self, response):
if self.timer:
self.timer.cancel()
self.future.set_result(response)
def set_exception(self, exception, traceback=None):
if self.timer:
self.timer.cancel()
self.future.set_exception(exception, traceback)
def on_timeout(self):
self.set_exception(TimeoutError("Request timed out after %d seconds." % INVOCATION_TIMEOUT))
示例2: test_set_result
def test_set_result(self):
f = Future()
def set_result():
f.set_result("done")
Thread(target=set_result).start()
self.assertEqual(f.result(), "done")
示例3: test_running
def test_running(self):
f = Future()
self.assertTrue(f.running())
f.set_result("done")
self.assertFalse(f.running())
示例4: test_done
def test_done(self):
f = Future()
self.assertFalse(f.done())
f.set_result("done")
self.assertTrue(f.done())
示例5: test_callback_throws_exception
def test_callback_throws_exception(self):
f = Future()
def invalid_func():
raise RuntimeError("error!")
f.add_done_callback(invalid_func)
f.set_result("done") # should not throw exception
示例6: test_continue_with_on_success
def test_continue_with_on_success(self):
f = Future()
f.set_result(1)
def continuation(future):
return future.result() + 1
result = f.continue_with(continuation).result()
self.assertEqual(2, result)
示例7: test_combine_futures_exception
def test_combine_futures_exception(self):
f1, f2, f3 = Future(), Future(), Future()
combined = combine_futures(f1, f2, f3)
e = RuntimeError("error")
f1.set_result("done")
f2.set_result("done")
f3.set_exception(e)
self.assertEqual(e, combined.exception())
示例8: test_continue_with_on_failure
def test_continue_with_on_failure(self):
f = Future()
f.set_exception(RuntimeError("error"))
def continuation(future):
if future.is_success():
return 0
else:
return 1
result = f.continue_with(continuation).result()
self.assertEqual(1, result)
示例9: test_add_callback_after_completion_with_success
def test_add_callback_after_completion_with_success(self):
f = Future()
f.set_result("done")
counter = [0]
def callback(future):
counter[0] += 1
self.assertEqual(future.result(), "done")
f.add_done_callback(callback)
self.assertEqual(counter[0], 1)
示例10: test_continue_with_throws_exception
def test_continue_with_throws_exception(self):
f = Future()
e = RuntimeError("error")
def continue_func(f):
raise e
n = f.continue_with(continue_func)
f.set_result("done")
self.assertFalse(n.is_success())
self.assertEqual(n.exception(), e)
示例11: test_combine_futures
def test_combine_futures(self):
f1, f2, f3 = Future(), Future(), Future()
combined = combine_futures(f1, f2, f3)
f1.set_result("done1")
self.assertFalse(combined.done())
f2.set_result("done2")
self.assertFalse(combined.done())
f3.set_result("done3")
self.assertEqual(combined.result(), ["done1", "done2", "done3"])
示例12: test_add_callback_after_completion_with_error
def test_add_callback_after_completion_with_error(self):
f = Future()
error = RuntimeError("error")
f.set_exception(error, None)
counter = [0]
def callback(future):
counter[0] += 1
self.assertEqual(future.exception(), error)
f.add_done_callback(callback)
self.assertEqual(counter[0], 1)
示例13: test_add_callback_with_success
def test_add_callback_with_success(self):
f = Future()
e = Event()
def set_result():
f.set_result("done")
def callback(future):
self.assertEqual(future.result(), "done")
e.set()
f.add_done_callback(callback)
Thread(target=set_result).start()
self.assertTrue(e.wait(timeout=5))
示例14: __init__
def __init__(self, request, partition_id=-1, address=None, connection=None, timeout=INVOCATION_TIMEOUT):
self._event = threading.Event()
self.timeout = timeout + time.time()
self.address = address
self.connection = connection
self.partition_id = partition_id
self.request = request
self.future = Future()
示例15: test_set_exception
def test_set_exception(self):
f = Future()
exc = []
def set_exception():
try:
{}["invalid_key"]
except KeyError as e:
exc.append(sys.exc_info())
f.set_exception(e, sys.exc_info()[2])
Thread(target=set_exception).start()
exception = f.exception()
traceback = f.traceback()
exc = exc[0]
self.assertEqual(exc[1], exception)
self.assertEqual(exc[2], traceback)