当前位置:   article > 正文

asyncio 使用记录_python asyncio async ensure声明的函数能有返回值吗?

python asyncio async ensure声明的函数能有返回值吗?
 
  1. import asyncio
  2. import time
  3. import requests
  4. from concurrent.futures import ALL_COMPLETED,FIRST_COMPLETED,FIRST_EXCEPTION
  5. async def task2():
  6. print('task2')
  7. # res = requests.get('https://www.baidu.com',verify=False)
  8. # print(res.status_code)
  9. await asyncio.sleep(3)
  10. # print('sleep 3 task2')
  11. return 111
  12. async def task(num):
  13. print('task')
  14. t = loop.create_task(task2())
  15. await asyncio.sleep(num)
  16. await t
  17. print('sleep 2 task')
  18. if num == 2:
  19. raise NotImplemented
  20. return f'task res {num}'
  21. async def main(loop):
  22. # 正确的写法1
  23. task1 = [loop.create_task(task(1)) for _ in range(1100)] # 3.7 可用 asysico.create_task()
  24. task22 = loop.create_task(task2())
  25. for t in task1:
  26. await t
  27. await task22
  28. # 写法2
  29. # res1 = asyncio.gather(task(1))
  30. # res2 = asyncio.gather(task(1))
  31. # res3 = asyncio.wait([task(1),task(3)],return_when=FIRST_COMPLETED)
  32. # res3 = asyncio.wait([task(1) for i in range(100)],return_when=ALL_COMPLETED) # wait方式如果不一次写全 会导致异步失败
  33. # res4 = asyncio.wait([task(1) for i in range(100)],return_when=ALL_COMPLETED)
  34. # # await res1
  35. # # await res2
  36. # pending,done = await res3
  37. # print(pending,done,"*"*11)
  38. # res = await asyncio.gather(task(1))
  39. # res = await asyncio.gather(task(1))
  40. # print(res)
  41. # t1 = [asyncio.ensure_future(task(5)) for _ in range(20) ]
  42. # t2 = asyncio.ensure_future(task2())
  43. # for t in t1:
  44. # res = await t
  45. # print(f'res = {res}')
  46. # await t2
  47. # 错误的写法1 假异步
  48. # await loop.create_task(task())
  49. # await loop.create_task(task2())
  50. # 错误的写法2 假异步
  51. # await asyncio.ensure_future(task2())
  52. # await asyncio.ensure_future(task2())
  53. if __name__ == '__main__':
  54. # 创建任务的方法
  55. # 1. asyncio.gather 全称黑盒 只告诉结果
  56. # 2. asyncio.wait 状态+结果 有参数 选择返回的时机 done,pending = awit res
  57. # 3. loop.create_task
  58. # 4. asyncio.ensure_future 无返回值
  59. # 5. asyncio.create_task # 3.7
  60. # 6. asyncio.shield # 确保任务完成 即使cancel 也会被执行得到结果
  61. start = time.perf_counter()
  62. loop = asyncio.get_event_loop()
  63. # method 1
  64. # tasks =asyncio.gather(*[task(_) for _ in range(1000)],return_exceptions=True) # 方式一 gather
  65. # loop.run_until_complete(tasks)
  66. # print(tasks.result())
  67. # # end 1
  68. # method 2
  69. # tasks = loop.create_task(task())
  70. # loop.run_until_complete(tasks)
  71. # method 3 asyncio.wait
  72. tasks = asyncio.wait([task2() for _ in range(1000)])
  73. done,pending = loop.run_until_complete(tasks)
  74. print(done)
  75. # method 4 main 中 创建
  76. # loop.run_until_complete(loop.create_task(main(loop)))
  77. # loop.run_forever()
  78. loop.time()
  79. print(f'耗时{time.perf_counter()-start}s')
'
运行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/832756
推荐阅读
相关标签
  

闽ICP备14008679号