import asyncio,sys
@asyncio.coroutine
def f(fu,n):
count=0
for i in range(1,n+1):
count=count+i
yield from asyncio.sleep(4)
fu.set_result("first coroute"+str(count))
@asyncio.coroutine
def s(fu,n):
count=1
for i in range(2,n+1):
count*=i
yield from asyncio.sleep(4)
fu.set_result("second coroute"+str(count))
def got_result(fu):
print(fu.result())
if name=="main":
n1=10
n2=10
loop=asyncio.get_event_loop()
fu1=asyncio.Future()
fu2=asyncio.Future()
tasks=[
f(fu1,n1),
s(fu2,n2)]
fu1.add_done_callback(got_result)
fu2.add_done_callback(got_result)
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
asyncio.Future()
fu.add_done_callback(xxx)
fu.set_result()
fu.result()
网友评论