cancel(): 取消future的执行,调度回调函数 result(): 返回future代表的结果 exception(): 返回future中的Exception add_done_callback(fn): 添加一个回调函数,当future执行的时候会调用这个回调函数 remove_done_callback(fn): 从“call whten done”列表中移除所有callback的实例 set_result(result): 将future标为执行完成,并且设置result的值 set_exception(exception): 将future标为执行完成,并设置Exception
""" Asyncio.Futures - Chapter 4 Asynchronous Programming """ import asyncio import sys
@asyncio.coroutine def first_coroutine(future, N): """前n个数的和""" count = 0 for i in range(1, N + 1): count = count + i yield from asyncio.sleep(4) future.set_result("first coroutine (sum of N integers) result = " + str(count))
@asyncio.coroutine def second_coroutine(future, N): count = 1 for i in range(2, N + 1): count *= i yield from asyncio.sleep(3) future.set_result("second coroutine (factorial) result = " + str(count))
def got_result(future): print(future.result())
if name == "main": N1 = int(sys.argv[1]) N2 = int(sys.argv[2]) loop = asyncio.get_event_loop() future1 = asyncio.Future() future2 = asyncio.Future() tasks = [ first_coroutine(future1, N1), second_coroutine(future2, N2)] future1.add_done_callback(got_result) future2.add_done_callback(got_result) loop.run_until_complete(asyncio.wait(tasks)) loop.close()
python asy.py 2 2 first coroutine (sum of N integers) result = 3 second coroutine (factorial) result = 2
python asy.py 4 4 first coroutine (sum of N integers) result = 10 second coroutine (factorial) result = 24