async for的使用

import random
import asyncio


async def random_number_gen(delay, start, end):
    while True:
        yield random.randint(start, end)
        await asyncio.sleep(delay)


async def main():
    async for i in random_number_gen(1, 0, 100):
        print(i)


try:
    print("Starting to print out random numbers...")
    print("Shut down the application with Ctrl+C")
    asyncio.run(main())
except KeyboardInterrupt:
    print("Closed the main loop..")
posted @ 2019-06-15 16:57  公众号python学习开发  阅读(1997)  评论(0编辑  收藏  举报