Asyncio的案例运用

###############asyncio: example1 ###################
import asyncio

async def main():
    print('hello')
    await asyncio.sleep(1)
    print('world')

coro = main()

asyncio.run(coro)

 

###############asyncio: example2 ###################
import asyncio
import time

async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    print(f'started at {time.strftime("%X")}')

    await say_after(1, 'hello')
    await say_after(2, 'world') # 这两个还是只能依序执行

    print(f'finished at {time.strftime("%X")}')

asyncio.run(main())

 

################asyncio: example3 ###################
import asyncio
import time


async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    task1 = asyncio.create_task(say_after(1, 'hello'))
    task2 = asyncio.create_task(say_after(2, 'world'))  # 这下task1, task2就能并行运行了

    print(f'finished at {time.strftime("%X")}')
    await task1
    await task2

    print(f'finished at {time.strftime("%X")}')


asyncio.run(main())

 

################asyncio: example4 ###################
import asyncio
import time


async def say_after(delay, what):
    await asyncio.sleep(delay)
    return f'{what} - {delay}'

async def main():
    task1 = asyncio.create_task(say_after(1, 'hello'))
    task2 = asyncio.create_task(say_after(2, 'world'))  # 这下task1, task2就能并行运行了

    print(f'finished at {time.strftime("%X")}')
    ret1 = await task1
    ret2 = await task2

    print(ret1)
    print(ret2)

    print(f'finished at {time.strftime("%X")}')


asyncio.run(main())

 

################asyncio: example5 ###################
import asyncio
import time


async def say_after(delay, what):
    await asyncio.sleep(delay)
    return f'{what} - {delay}'

async def main():
    task1 = asyncio.create_task(say_after(1, 'hello'))
    task2 = asyncio.create_task(say_after(2, 'world'))  # 这下task1, task2就能并行运行了

    print(f'finished at {time.strftime("%X")}')
    ret = await asyncio.gather(task1, task2)  # 当任务很多时,就用gather(gather的参数事若干个coroutine或者task, 甚至可以是future),免得写很多个await。这个gather返回的是一个list
                                              # 注意: gather不是coroutine, 但是gather会返回future对象,future对象也是可以用await的
    print(ret)

    print(f'finished at {time.strftime("%X")}')


asyncio.run(main())

 

################asyncio: example6 ###################
import asyncio
import time


async def say_after(delay, what):
    await asyncio.sleep(delay)
    return f'{what} - {delay}'

async def main():
    task1 = asyncio.create_task(say_after(1, 'hello'))
    task2 = asyncio.create_task(say_after(2, 'world'))  # 这下task1, task2就能并行运行了

    print(f'finished at {time.strftime("%X")}')
    ret = await asyncio.gather(
        say_after(1, 'hello'),  # 可以不手动先建立task,直接把coroutine传递给gather
        say_after(2, 'world')  # 这种方式也是只用2 秒
    )
    print(ret)

    print(f'finished at {time.strftime("%X")}')


asyncio.run(main())

 

################asyncio: example7 ###################

import asyncio


async def main():
    await asyncio.sleep(0.1)

asyncio.run(main())

 

参考资料:【python】asyncio的理解与入门,搞不明白协程?看这个视频就够了

 

posted @ 2024-03-19 17:27  AlphaGeek  阅读(8)  评论(0)    收藏  举报