Making async for loops in Python

后端 未结 3 905
耶瑟儿~
耶瑟儿~ 2021-01-02 05:33

The following code outputs as follows:

1 sec delay, print \"1\", 
1 sec delay, print \"2\", 
1 sec delay, print \"1\", 
1 sec delay, print \"2\"
3条回答
  •  执笔经年
    2021-01-02 06:20

    With the aysncio library you can use aysncio.gather()

    loop.run_until_complete(asyncio.gather(
      first(),
      second()
    ))
    

    This can come in handy if you are also sending HTTP requests in parallel:

    loop.run_until_complete(asyncio.gather(
      request1(),
      request2()
    ))
    

提交回复
热议问题