asyncio 範例 - jenhaoyang/backend_blog GitHub Wiki

Forever Event Loop

import asyncio

async def work():
    while True:
        await asyncio.sleep(1)
        print("Task Executed")

loop = asyncio.get_event_loop()
try:
    asyncio.ensure_future(work())
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    print("Closing Loop")
    loop.close()

參考:
https://tutorialedge.net/python/concurrency/asyncio-event-loops-tutorial/