Python基础:15_协程

import asyncio

async def func1():
    print("Im A")
    await asyncio.sleep(3)
    print("Im A")

async def func2():
    print("Im B")
    await asyncio.sleep(2)
    print("Im B")

async def func3():
    print("Im C")
    await asyncio.sleep(4)
    print("Im C")

async def main():
    # 第一中写法
    # f1 = func1()
    # await f1  # 一般await挂起操作放在协程对象前面
    # f2 = func2()
    # await f2
    # f3 = func3()
    # await f3

    # (推荐)第二种写法
    tasks = [
        func1(),
        func2(),
        func3()
        # python3.8以后用下面的写法
        # asyncio.create_task(func1()),
        # asyncio.create_task(func2()),
        # asyncio.create_task(func3())
    ]
    await asyncio.wait(tasks)

if __name__ == '__main__':
    asyncio.run(main())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值