美文网首页随笔-生活工作点滴
python asyncio 协程futures结果回调(并行编

python asyncio 协程futures结果回调(并行编

作者: SkTj | 来源:发表于2019-07-28 13:56 被阅读0次

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()

相关文章

网友评论

    本文标题:python asyncio 协程futures结果回调(并行编

    本文链接:https://www.haomeiwen.com/subject/putshctx.html