php-app开发接口加密的示例分析
281
2022-09-03
协程的嵌套(协程使用场景)
# coding: utf-8# @Time : 2022-05-17 13:15# @Author : AngDHimport asyncioimport timenow = lambda: time.time()async def task1(): await asyncio.sleep(1) print("task1 done") return "task1"async def task2(): await asyncio.sleep(1) print("task2 done") return "task2"async def task3(): await asyncio.sleep(1) print("task3 done") return "task3"async def main(): start = now() tasks = [task1(), task2(), task3()] # 1 # dones, pendings = await asyncio.wait(tasks) # for task in dones: # print("task result:", task.result()) """异步运行 异步输出""" # 2 # for task in asyncio.as_completed(tasks): # result = await task # print("task result:", result) """执行顺序=结果顺序""" # 3 result = await asyncio.gather(*tasks) print(result) """异步运行,同步输出,结果顺序和传参顺序一致""" print("耗时:", now() - start)asyncio.run(main())
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~