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..")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM